Hans Wassink
Hans Wassink

Reputation: 2577

htaccess conditional rewrite

I am fiddling around with .htaccess and mod_rewrite. I have a site that has two types of URLs which I want to rewrite:

I want to rewrite them to:

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

Answers (3)

Shef
Shef

Reputation: 45589

Try this:

RewriteRule ^news/.+/([^/]*)$ /index.php?nav=41&intNewsId=$1 [L]
RewriteRule ^.+/([^/]*)$ /index.php?nav=$1 [L]

Upvotes: 1

Will Vousden
Will Vousden

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

Rakesh Sankar
Rakesh Sankar

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

Related Questions