mod_rewrite always uses index.php - apache

I'm rebuilding one of my websites (redesign + pdo instead of regular mysql).
So far so good, everything works out, except from 1 RewriteRule in .htaccess.
All of my url-rewrites are going fine with this kind of rule:
RewriteRule pages/(.*)/ index.php?type=page&page=$1&%{QUERY_STRING}
RewriteRule pages/(.*) index.php?type=page&page=$1&%{QUERY_STRING}
(note: making use of index.php)
For my shopping cart, I want to use a clean, separate page, with this rule:
RewriteRule ^shop/shopping-cart-overview/$ /pages/shoppingcart/overview.php
RewriteRule ^shop/shopping-cart-overview$ /pages/shoppingcart/overview.php
Problem: instead of using the file "/pages/shoppingcart/overview.php", it still uses index.php as in the other rules.
I uploaded full htaccess in txt, so you can read it yourself:
www.mymobisite.be/htaccess.txt
Thank you in advance for your time, help and advice!
Tom V.

The problem is the order of the rules. The shop/shopping-cart comes before the pages rule.
The first rule rewrites shop/shopping-cart-overview/ to /pages/shoppingcart/overview.php, which in turn is rewritten to index.php?type=page&page=shoppingcart/overview.php.
You should either reorder your rewrite rules or use the [L] modifier (L=last).
RewriteRule ^shop/shopping-cart-overview/$ /pages/shoppingcart/overview.php [L]
RewriteRule ^shop/shopping-cart-overview$ /pages/shoppingcart/overview.php [L]

Related

mod_rewrite 301 redirect from old urls to new

Website has changed its url names due to SEO reasons, e.g. it was:
/category/filter1/f00/filter2/123/filter3/100-500/filter4/36.html
now:
/category/color/red/size/big/price/100-500/style/classic.html
I know the old and new names, they're fixed. Please help me to build a rewrite rule which will result in 301 redirect from old urls to new. I did research and I see that I cannot make it using RewriteMap for example, so I ended up making something like RewriteRule (.*)filter1(.*) $1color$2 [L] etc. Not only I don't like the way it looks, but also it doesn't give me a 301 redirect.
UPDATE: Note that at the moment I have several rules, one per filter name/value, e.g.:
RewriteEngine on
# make sure it's a catalog URL, not anything else
RewriteCond %{REQUEST_URI} !^/(category1|category2|category3|category4)
RewriteRule .* - [L]
# rewrite filter names
RewriteRule (.*)filter1(.*) $1color$2 [L]
RewriteRule (.*)filter2(.*) $1price$2 [L]
...etc...
It works as expected - changing all the names in URL, but setting R flag causes the stop on first rule and redirect to URL like:
/var/www/vhosts/site/htdocs/category/color/red/filter2/123/ etc...
I separated rules because any of filters may or may not exist in the URL. I will greatly appreciate the better solution.
Here is my own answer: it is possible to do with environment variables. We need to replace old filter names and values with new ones, and then make only one 301 redirect to new URL. Here what I've done using mod_rewrite and environment variables:
RewriteEngine on
RewriteRule /filter1/ - [E=filters:/color/]
RewriteRule /f00[.\/] - [E=filters:%{ENV:filters}red]
RewriteRule /0f0[.\/] - [E=filters:%{ENV:filters}green]
RewriteRule /00f[.\/] - [E=filters:%{ENV:filters}blue]
RewriteRule /filter2/ - [E=filters:%{ENV:filters}/size/]
RewriteRule /123[.\/] - [E=filters:%{ENV:filters}big]
RewriteRule /32[.\/] - [E=filters:%{ENV:filters}small]
RewriteRule /filter3/([^/^\.]+) - [E=filters:/price/$1]
RewriteRule /filter4/ - [E=filters:%{ENV:filters}/style/]
RewriteRule /36[.\/] - [E=filters:%{ENV:filters}classic]
RewriteRule /37[.\/] - [E=filters:%{ENV:filters}urban]
RewriteCond %{REQUEST_URI} ^/(category1|category2|category3|category4)/
RewriteCond %{ENV:filters} !^$
RewriteRule ^([^/]+)/ /$1%{ENV:filters}.html [L,R=301]
Basically, I've reformatted whole the URL in environment variable filters then checked if it's a category and not some else part of the website, and finally made redirect to this category+filters variable, appended .html at the end.
Even though the new URL looks prettier to a human, I'm not sure if there's a need to change the existing URL for SEO reasons.
To get a redirect instead of a rewrite, you must use the R|redirect flag. So your rule would look like
RewriteRule (.*)filter1(.*) $1color$2 [R,L]
But if you have multiple redirects, this might impact your SEO results negatively, see Chained 301 redirects should be avoided for SEO , but Google will follow 2 or 3 stacked redirects
Remember that ideally you shouldn’t have any stacked redirects or even a single redirect if you can help it, but if required Google will follow chained redirects
But every additional redirect will make it more likely that Google won’t follow the redirects and pass PageRank
For Google keep it to two and at a maximum three redirects if you have to
Bing may not support chained redirects at all
This means try to replace multiple filters at once
RewriteRule ^(.*)/filter1/(.*)/filter2/(.*)$ $1/color/$2/size/$3 [R,L]
and so on.
When the filters may come in an arbitrary order, you may use several rules and do a redirect at the end
RewriteRule ^(.*)filter1(.*)$ $1color$2 [L]
RewriteRule ^(.*)filter2(.*)$ $1price$2 [L]
RewriteRule ^(.*)filter3(.*)$ $1size$2 [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ %{REQUEST_URI} [R,L]
RewriteCond with REDIRECT_STATUS is there to prevent an endless loop.
When it works as it should, you may replace R with R=301. Never test with R=301.
A final note, be very careful with these experiments. I managed to kill my machine twice (it became unresponsive and I had to switch off) during tests.

How to create an htaccess rewrite rule with part of parameter as identifier

I need some help with a rewrite rule I am struggling with.
I have the existing rule below, which works well and redirects as follows:
www.site.com/page.php?type=1&category=2&cond=3
redirected to
www.site.com/1/2/3/
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /page.php?type=$1&category=$2&cond=$3 [L]
Now I need to create one new rule that will not interfere with the rule above.
So this url:
www.site.com/page.php?type=1&category=2&page=page-1
should redirect to:
www.site.com/1/2/page-1/
Obviously, the browser should make somehow the difference between www.site.com/1/2/3/ and www.site.com/1/2/page-1/.
This difference can be the part of the third parameter which will be always constant: page- .
I know I need to somehow modify this part of the new rule ([^/]*) but nothing I have tried so far does to job.
Any suggestions how should I accomplish this?
Have these 2 rules in this order:
RewriteRule ^([^/]+)/([^/]+)/(page-[^/]+)/?$ /page.php?type=$1&category=$2&cond=$3 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /page.php?type=$1&category=$2&cond=$3 [L,QSA]
You should be able to do your rules this way.
RewriteRule ^([^/]*)/([^/]*)/page-([^/]*)/?$ /page.php?type=$1&category=$2&page=page-$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /page.php?type=$1&category=$2&cond=$3 [L]

How to add "everything else" rule to mod_rewrite

How can I make mod_rewrite redirect to a certain page or probably just throw 404 if no other rules have been satisfied? Here's what I have in my .htaccess file:
RewriteEngine on
RewriteRule ^\. / [F,QSA,L]
RewriteRule ^3rdparty(/.*)$ / [F,QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^((images|upload)/.+|style.css)$ $1 [L]
RewriteRule ^$ special [QSA]
RewriteRule ^(special|ready|building|feedback)/?$ $1.php [QSA,L]
RewriteRule ^(ready|building)/(\d+)/?$ show_property.php?type=$1&property_id=$2 [QSA,L]
RewriteRule . error.php?code=404 [QSA,L]
This is supposed, among other things, to send user to error.php if he tries to access anything that was not explicitly specified here (by the way, what is the proper way to throw 404?). However, instead it sends user from every page to error.php. If I remove the last rule, everything else works.
What am I doing wrong?
What is happening is that when you are doing a rewrite, you then send the user to the new URL, where these rewrite rules are then evaluated again. Eventually no other redirectoin rules will be triggered and it will get to the final rule and always redirect to the error.php page.
So you need to put some rewrite conditions in place to make this not happen.
The rewrite engine loops, so you need to pasthrough successful rewrites before finally rewriting to error.php. Maybe something like:
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/(special|ready|building|feedback|show_property)\.php
RewriteCond %{REQUEST_URI} !^/((images|upload)/.+|style.css)$
RewriteRule ^ error.php?code=404 [QSA,L,R=404]
Each condition makes sure the URI isn't one of the ones your other rules have rewritten to.
The R=404 will redirect to the error.php page as a "404 Not Found".
Unfortunatelly, it didn't work - it allows access to all files on the server (presumably because all conditions need to be satisfied). I tried an alternate solution:
Something else must be slipping through, eventhough when I tested your rules plus these at the end in a blank htaccess file, it seems to work. Something else you can try which is a little less nice but since you don't actually redirect the browser anywhere, it would be hidden from clients.
You have a QSA flag at the end of all your rules, you could add a unique param to the query string after you've applied a rule, then just check against that. Example:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^((images|upload)/.+|style.css)$ $1?_ok [L,QSA]
then at the end:
RewriteCond %{QUERY_STRING} !_ok
RewriteRule ^ error.php?code=404&_ok [QSA,L,R=404]
In theory if none of the rules are matched (and the requested URL does not exist), it's already a 404. So I think the simplest solution is to use an ErrorDocument, then rewrite it:
RewriteEngine On
ErrorDocument 404 /404.php
RewriteRule ^404.php$ error.php?code=404 [L]
# All your other rules here...
You can do the same for any other HTTP error code.
The problem here is that after the mod_rewrite finishes rewriting the URL, it is resubmitted to the mod_rewrite for another pass. So, the [L] flag only makes the rule last for the current pass. As much better explained in this question, mod_rewrite starting from Apache version 2.3.9, now supports another flag - [END], that makes the current mod_rewrite pass the last one. For Apache 2.2 a number of solutions are offered, but since one of them was a bit clumsy and another didn't work, my current solution is to add another two rules that allow a specific set of files to be accessed while sending 404 for everything else:
RewriteRule ^((images|upload)/.+|style.css|(special|ready|building|feedback|property).php)$ - [QSA,L]
RewriteRule .* - [QSA,L,R=404]
I think your last rule should be
RewriteRule ^(.*)$ error.php?code=404&query=$1 [QSA,L]
You could leave out the parenthesis and the $1 parameter, but maybe it's useful to know, what the user tried to achieve.
Hope, this does the trick!

Apache URL Rewriting for multiple dynamic folder levels

Wondering if somebody can help me write some RewriteRule's for my website.
Take a look at the following URLs and see how I need to rewrite them.
http://www.example.com/essays-and-reports/
does not need to be re-written, it is a physical folder on the web server.
http://www.example.com/essays-and-reports/business/
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/dynamic_name2
needs to rewrite to (root)/first_level_template.php
http://www.example.com/essays-and-reports/business/financial-reports/
needs to rewrite to (root)/second_level_template.php
http://www.example.com/essays-and-reports/blah/financial-reports/C_B_2413_Report_on_savings.php
needs to rewrite to (root)/final_level_template.php
Note the rules must work regardless of a trailing slash. To sum-up, there are three levels which I need to re-write to their relevant template. None of above exists physically on the server including the PHP file for final level. The only thing that exists is the essays-and-reports folder which is main folder for the website.
I tried something like this but I get compile errors in the log.
RewriteRule ^([^/]+)/([^/]+)/?$ /second-level-template\.php [L]
If you could help me write the rules I need - I appreciate it greatly.
EDIT:
This code kind of works but it also rewrites the essays-and-reports folder which I don't want...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /essays-and-dissertations/
RewriteRule ^[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
RewriteRule ^[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^[^/]+/?$ /first_level_template.php [L]
You could do this with 3 rules:
RewriteRule ^essays-and-reports/[^/]+/?$ /first_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/?$ /second_level_template.php [L]
RewriteRule ^essays-and-reports/[^/]+/[^/]+/[^/]+/?$ /final_level_template.php [L]
You don't need to escape the dot in your redirect target, since that's not a regular expression.

Trouble with advanced .htaccess redirect

I'm migrating a custom coded blog over to Wordpress, and have to set up a redirect that will handle all of the blog posts.
I need to redirect from this:
/oldblogdirectory/Old_Blog_Posts_Looked_Like_This.htm
to:
/newblogdirectory/new-blog-posts-look-like-this/
Any ideas on the regex for a redirect like this?
Gumbo's approach is certainly the way to do it. I made two test directories:
oldblogdir/archives/blog_posts_look_like_this.htm
newblogdir/archives/blog-posts-look-like-this
And the following RewriteRules redirect successfully. They are only slightly changed to Gumbo's proposal:
RewriteEngine on
RewriteBase /
RewriteRule ^(oldblogdir/archives/[^_]*)_(.*) $1-$2 [N]
RewriteRule ^oldblogdir/archives/(.*?)\.htm$ newblogdir/archives/$1 [R,NC,L]
Note that the [N] causes the .htaccess file to be re-evaluated until the RegEx no longer matches. Therefore you should put it at the very top of the file.
Try this mod_rewrite rules:
RewriteEngine on
RewriteRule ^(oldblogdirectory/[^_]*)_([^_]*)_(.*) /$1-$2-$3 [N]
RewriteRule ^(oldblogdirectory/[^_]*)_(.*) /$1-$2
RewriteRule ^oldblogdirectory/(.+)\.htm$ /newdirectory/$1/ [L,R=301]
But for the uppercase to lowercase conversion you’ll either need a mapping like the internal tolower function or you use PHP for both.