Forum Moderators: phranque

Message Too Old, No Replies

Having some trouble with mod rewrite

Looking for some assistance with mod_rewrite.

         

Stewart81

2:24 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



Hi. I am attempting to use mod_rewrite for a site.

The code which converts the URL to a new URL works great but the code to actually send the URL to the write place isn't working quite right.

This is what I have:

rewritecond %{REQUEST_URI} !^/products/
RewriteRule (.*)\.htm$ /proddetail.php?prod=$1
rewritecond %{REQUEST_URI} ^/products/
RewriteRule (.*)\.htm$ /products.php?cat=$1

The first part is supposed to send any requests that end in .htm and are not in the products directory to proddetail.php which works fine.

The second part is using rewritecond to limit it to products directory and then what ever ends in .htm sends to products.php?cat= and then the # before .htm.

This one does send it to products.php but it is not processing the id of the category. It basically gets "/products.php?cat=" every time.

I figure it's probably something fairly obvious but I can't figure it out.

Thanks for any help you can provide :)

jdMorgan

2:38 pm on Jan 27, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like your second rule *should* work, except that "products/" will be prepended to the value of "cat=".

That is, a request for the URL "example.com/products/123.htm" should be rewritten to the server filepath "/products.php?cat=products/123" as it stands right now.

I doubt it will fix your problem (which isn't obvious, given that no example URLs were provided), but a more-correct way to code the second rule would be


RewriteRule ^products/([^.]+)\.htm$ /products.php?cat=$1

That is, the RewriteCond is not needed in this rule, and the "products/" path must be excluded from the parentheses which capture the value of the $1 back-reference.

Note that in a .htaccess context, the URL-path 'seen' by RewriteRule is stripped of the path to the current .htaccess directory, which in this case is "/". So the leading slash won't appear in the URL-path seen by the RewriteRule, and is therefore not present in the pattern I used here.

Jim

Stewart81

2:45 pm on Jan 27, 2009 (gmt 0)

10+ Year Member



I used that rewriterule and it worked perfectly. Thank you for the help.

Stewart