Reputation: 2577
I am fiddling around with .htaccess and mod_rewrite. I have a site that has two types of URLs which I want to rewrite:
/index.php?nav=$2
/index.php?nav=41&intNewsId=$3
-- 41 is static, the news nav is always 41I want to rewrite them to:
/pagename/id
/news/pagename/id
I already made a piece of code that works, BUT if I add the last line the second line stops working, and I can imagine thats because the conditions in the third block are also true for the second block. But I cant figure out how to use conditions right. (Both blocks work individual)
Options +FollowSymlinks
RewriteEngine on
# Reroute rules that end on /
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$ /$1/$2/ [R]
# Make the system understand pagename/96
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=$2
# Make the system understand news/pagename/99
RewriteRule ^(.*)\/(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=41&intNewsId=$3
I tried everything I could think of, but I'm not too familiar with this regex style of typing or conditional blocks in htaccess.
Solution: I fixed my own code, I just stripped the second $ so the condition didnt interfere with the last one
Options +FollowSymlinks
RewriteEngine on
# Reroute rules that end on /
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$ /$1/$2/ [R]
# Make the system understand pagename/96
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/ /index.php?nav=$2
# Make the system understand news/pagename/99
RewriteRule ^(.*)\/(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=41&intNewsId=$3
Thanks for the answers all!
Upvotes: 2
Views: 462
Reputation: 45589
Try this:
RewriteRule ^news/.+/([^/]*)$ /index.php?nav=41&intNewsId=$1 [L]
RewriteRule ^.+/([^/]*)$ /index.php?nav=$1 [L]
Upvotes: 1
Reputation: 33348
Another approach, perhaps slightly more concise:
RewriteRule ^pagename/(\d+)$ index.php?nav=$1
RewriteRule ^news/pagename/(\d+)$ index.php?nav=41&intNewsId=$1
Upvotes: 1
Reputation: 9415
Try this:
# Make the system understand pagename/96
RewriteCond %{REQUEST_URI} ^/pagename/([0-9]*)
RewriteRule .* /index.php?nav=%1
# Make the system understand news/pagename/99
RewriteCond %{REQUEST_URI} ^/news/pagename/([0-9]*)
RewriteRule .* /index.php?nav=41&intNewsId=%1
Upvotes: 0