Marthinus Strydom
Marthinus Strydom

Reputation: 267

RewriteRule first rewrite is working but not the next one

I need to change country.cfm?country=xxx to xxx.html and hotel.cfm?hotel=yyy to yyy.html

I have the following in my .htacess file:

# change all the country pages
RewriteRule ^([^/]*)\.html$ /country.cfm?country=$1 [L]

# change all the hotel pages
RewriteRule ^([^/]*)\.html$ /hotel.cfm?hotel=$1 [L]

The problem is that country is working but not hotel. If I put the hotel rewriterule before the country then the hotel works but not the country.

If I go to hotel.cfm?hotel=yyy then I am redirected to country.cfm and visa versa if I swap the rewriterules around.

Upvotes: 0

Views: 15

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

Because you're using the exact same rewrite rule. How should the engine know that XXX.html is a country and YYY.html is a hotel when the rule matches both? Since you have the L flag it won't process the next one.

You need to have some difference in the URLs, for example hotel-XXX.html and then match to that. Then your rewrite will work.

For example:

# change all the country pages
RewriteRule ^country-([^/]*)\.html$ /country.cfm?country=$1 [L]

# change all the hotel pages
RewriteRule ^hotel-([^/]*)\.html$ /hotel.cfm?hotel=$1 [L]

for URLs like site.tld/hotel-name.html, or maybe

# change all the country pages
RewriteRule ^country/([^/]*)\.html$ /country.cfm?country=$1 [L]

# change all the hotel pages
RewriteRule ^hotel/([^/]*)\.html$ /hotel.cfm?hotel=$1 [L]

for URLs like site.com/hotel/name.html

Any difference in the match parts will suffice, even if it's a single letter in the beginning.

Upvotes: 2

Related Questions