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]
Related
I have url like this:
www.mydomain.com/index.php?param1=first¶m2=second¶m3=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¶m2=$2¶m3=$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¶m2=$2¶m3=$3 [QSA,L]
I have something like the following page:
www.domain.com/games/?puzzle=value
I'd like for it to show up as
www.domain.com/games/value
Here is the rewrite I wrote for forwarding to the original domain that causes a 500 Internal Server Error:
#forward games/value to games/?puzzle=value
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
Would someone help me solve my issue, I'm really bad at rewrites.
EDIT:
I'm currently using the following to allow parameters from foo on my main page.
# Internally forward /value to /index.php?foo=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
If I remove that then the following will work for what I want, but I need both instances to work.
#forward games/value to games/?puzzle=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
That is due to looping since URL after rewrite is also matching your pattern ^games/(.+?)/?$.
You need to exclude real files/directories from this rewrite rule:
#forward games/value to games/?puzzle=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/(.+?)/?$ /games/index.php?puzzle=$1 [L,QSA]
# Internally forward /value to /index.php?foo=value
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ /index.php?foo=$1 [L,QSA]
I have a rewrite rule where if you type:
www.whatever.com/profile-one.php
that is the url you see, but the actual template page is:
www.whatever.com/profile.php
Here are the lines in htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.whatever.com/$1$2/ [R=301,L]
RewriteRule profile-(.*)\.php$ profile.php?name=$1
My problem is, if you visit a url like:
www.whatever.com/second/profile-one.php
It still tries to bring back the profile page, even though the profile.php template does not exist in the "second" directory. What would be the best way to prevent this please?
This is happening due to wrong regex here. You need to use start anchor in your 2nd rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)([^/])$ http://www.whatever.com/$1$2/ [R=301,L]
RewriteRule ^profile-(.*)\.php$ profile.php?name=$1 [L,QSA]
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]
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.