Forum Moderators: phranque

Message Too Old, No Replies

Problem with one mod rewrite rule overwriting all others

         

dcampbell

5:12 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



I have the following redirects setup in my htaccess file:

Options +FollowSymlinks
RewriteEngine on

RewriteRule ^products/([\-_a-z0-9]+)\.php$ template-products.php?content=$1 [L]
RewriteRule ^information/([\-_a-z0-9]+)\.php$ template-information.php?content=$1 [L]
RewriteRule ^([\-_a-z0-9]+)\.php$ template.php?content=$1 [L]
ErrorDocument 404 /template.php?content=$1

The problem I have is the third rewrite rule captures all urls and therefore redirects all pages to template.php, ignoring the previous 2 rules and not using those templates.

How can I prevent the third rule from overwriting the previous 2?

Regards

dcampbell

5:17 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



An example of what I mean above:

mydomain.com/products/product1.php is using template.php instead of template-products.php

jdMorgan

5:30 pm on Oct 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, the problem is that the third rule rewrites its own URL-path, and those already rewritten by the previous two rules (mod_rewrite in .htaccess is recursive).

Add an exclusion to the third rule:


RewriteCond $1 !^template
RewriteRule ^([\-_a-z0-9]+)\.php$ template.php?content=$1 [L]

Jim

dcampbell

5:35 pm on Oct 20, 2008 (gmt 0)

10+ Year Member



Much obliged Jim, works like a charm.

Cheers

g1smd

6:23 pm on Oct 20, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



ErrorDocument 404 /template.php?content=$1

What fills the $1 server variable here? Is this a typo?