.htaccess mod_rewrite problem - shot myself in the foot?

Tag: .htaccess , mod-rewrite , url-rewriting , friendly-url Author: jsj005 Date: 2009-09-12

I have a page called category.php5 that uses $_GET["category"] to fetch the right content from the database. I want to pretty it up so is looks like:

sinaesthesia.co.uk/category/psoriasis

which would equal:

sinaesthesia.co.uk/category.php5?category=psoriasis

I have successfully done this sort of rewriting before, but since I can't get it to work now, I'm worred that I might have rules in place that are somehow screwing me. Here is my entire .htaccess file - the last couple of lines are supposed to do the above rewrite:

RewriteEngine On

#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]

#Translate default page to root
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]

#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5

#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2

#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2

#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]

#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]

#Translate /category/psoriasis to /category.php5?category=$1
RewriteRule ^category/(.*) /category.php5?category=$1 [L]

When I manually enter category.php5/category=psoriasis, it works great. When I enter category.php5/category/psoriasis, it doesn't. I'm worried that my line that changes a html/ to html? is an error, however when I take that line out, it still doesn't work. Everything else works as expected.

Is none of that rules applied on request of that URL?
Not that I know of - I get a straight 404 status.

Other Answer1

As a general strategy, strip down your file by commenting everything out, then re-enable things piece by piece until you find the rule that causes it to break.

Bear in mind that browsers sometimes cache redirects, so starting a fresh browser instance is a good idea. A useful service is http://web-sniffer.net/ which will give you an uncached result.

In general, looking at your set of redirects, this seems a little convoluted to me because of the chaining/sieve -type system you seem to be using. Instead, I would recommend starting with URLs that can be identified specifically, e.g. starting with

RewriteRule ^category/(.*) /category.php5?category=$1 [L]

and then leaving the rather messy .html => .php conversion stuff towards the end, if you end up needing it at all. I've done a lot of sites using redirects and have never needed generic conversions like that, so they should be avoidable.

Also bear in mind that .* means matching anything or nothing, so you probably want to use .+ instead.

comments:

Thanks, good advice there. I will go and do that and get back to you - in particular, the .+ is a pretty sound idea.
Okay, I tried stripping the file down to just #Translate /category/psoriasis to /category.php5?category=psoriasis RewriteRule ^category/(.+)$ /category.php5?category=$1 [L] and also tried, just in case #Translate /category/psoriasis to /category.php5?category=psoriasis RewriteRule ^(.+)category/(.+)$ /$1category.php5?category=$2 [L] and it won't work. It's such a simple rule!

Other Answer2

Ah: because I have a document called category.php5 and I'm trying to use category/psoriasis, the server tries to resolve that as category.php5/psoriasis, which fails. Fixed it now!