htaccess ignore specific directory in URI - treat same as root - regex

been looking for several hours now without success.
I'd like all urls containing /en/ to ignore this part but continue with all other rules in the htaccess (like php to no extension rule).
EXAMPLE
www.domain.com/en/gallery -> should point to gallery.php in my server root
www.domain.com/gallery -> should ALSO point to gallery.php in my server root
(will have to take care of duplicate content here somehow later)
www.domain.com/en/contact -> should point to contact.php in my root
Thank you
U P D A T E
Like mentioned in my question I'd like all other rules to still be running.
Only this rule does not work with below solution as it should work with
www.domain.com/en/category1 AND www.domain.com/category1 so with or without EN.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !(\.png|\.jpg|\.gif|\.jpeg|\.bmp|\.css|\.js|\.ico)$
RewriteRule ^([^/]+)/? /categories.php?rw=1&url=$1 [L,QSA]

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(?:en/)?([^/]+)/?$ $1.php [L]
UPDATE:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(png|jpe?g|gif|bmp|css|js|ico)$
RewriteRule ^(?:en/)?([^/]+)/?$ categories.php?rw=1&url=$1 [L,QSA]

Related

htaccess does not work properly

On my WebServer I have a htaccess file which looks like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?path=$1 [NC,L,QSA]
So all calls to my WebServer are redirected to the index.php file in my root directory with a path parameter which holds the requested file. Now look at these two calls:
domain.com/test/blub.php works -> path = test/blub.php
domain.com/test/ does not work -> path should be test
How can I achieve the the second call also works?
Remove this condition from your rule:
RewriteCond %{REQUEST_FILENAME} !-d
As this is skipping all real directories from this rule.
Use your rule as:
DirectorySlash Off
RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/?$ /index.php?path=$1 [NC,L,QSA]

configure htaccess to grab GET and not directory

I have url like this:
www.mydomain.com/index.php?param1=first&param2=second&param3=thirth
and I want to rewrite it to :
www.mydomain.com/first/second/third
.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)$ index.php?param1=$1&param2=$2&param3=$3 [QSA,L]
Problem is, that I am getting error 404 (I think that server is looking for directory /first/second/third instead of letting index.php in root dir to GET parameters from URL). Am I wrong? Or how to deal with this problem?
Your regex is incorrect actually. You're using 3 back-reference vales in target but only 2 values are being captured.
You can use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?param1=$1&param2=$2&param3=$3 [QSA,L]

htaccess effects all index pages, need only the root index page effected

I have the following code to forward the root index page like the following:
www.domain.com/index.php?foo=value -> www.domain.com/value
Here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
The problem is this forwards all my index pages to the main page and I really only want this code to effect the root.
I figured it would be as simple as adding the domain as part of the rule but it didn't work for me.
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/.]+)/?$ /index.php?foo=$1 [L,QSA]

Mapping URLs with mod rewrite

I migrated my site to a new platform & I want to map old URLs like this:
anecdotage.com/index.php?aid=13337
to this:
anecdotage.com/articles/13336/
[Note the offset of one in the IDs]
Can I do this in apache's mod_rewrite. Any help appreciated!
You can't do any math with mod_rewrite by itself, meaning you can match against 13337 and do something to it to change it to 13336 without employing some kind of `RewriteMap to externally subtract a number by 1, but this requires access to either server or vhost config.
So in the vhost/server config you have to setup the map:
RewriteMap subtract prg:/path/to/script/that/subtracts-by-one.sh
This subtracts-by-one.sh script takes a number as an input, subtracts 1 from it, then prints out the result. Then to use it in a rewrite rule:
# to make sure we aren't clobbering legit requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=([0-9]+)
RewriteRule ^/?index.php$ /articles/${subtract:%1}/? [L]
If you don't have access to rewrite maps, then you'll need to do it the hard way and enumerate all of your rewrites:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=12345
RewriteRule ^/?index.php$ /articles/12344/? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=13337
RewriteRule ^/?index.php$ /articles/13336/? [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} aid=19911
RewriteRule ^/?index.php$ /articles/19910/? [L]
etc.

htaccess for multiple optional parameters

My mission is to achieve
http://localhost/projectname/media/video
from
http://localhost/projectname/index.php?m=media&cmd=video
and also
http://localhost/projectname/home
http://localhost/projectname/index.php?m=home
Below the files helps to achieve my mission
RewriteRule ^([^/]*)$ /projectname/index.php?m=$1
RewriteRule ^([^/]*)/([^/]*)$ /projectname/index.php?m=$1&cmd=$2
the thing is that any external files such as javascript and css would not be taken into play.
i know my alternative is to put this is htaccess
RewriteRule ^([^/]*)/cmd/([^/]*)$ /projectname/index.php?m=$1&cmd=$2
which enables http://localhost/projectname/media/cmd/video
I would like to make it without the CMD. Any suggestions?
Change your rules to this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ projectname/index.php?m=$1&cmd=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ projectname/index.php?m=$1 [L,QSA]
And make sure your js, css, images files are using absolute path not the relative ones. Which means their paths should either start with http:// or /.
why not just pass everything to index.php and then handle/parse the path, e.g.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Then use
$_SERVER['PATH_INFO']
$_SERVER['ORIG_PATH_INFO']
$_SERVER['QUERY_STRING']
http://php.net/manual/en/reserved.variables.server.php