简体   繁体   中英

Conflicting mod_rewrite rules

This is the full set of rewrite code in my htaccess file, but I'm only having problems with a conflict in the last 3 rules. The final rule writes the URL I want, but returns travel.php instead of travel2.php

RewriteEngine On
Options +FollowSymLinks
RewriteCond %{http_host} ^www.example.com [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]

RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteRule ^journal/([^/]*)$ /journal2.php?url=$1 [L]

RewriteRule ^travel/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel.php?country=$1&url_string=$2
RewriteRule ^travel/([a-zA-Z0-9-]*)$ /travel2.php?cat=$1
RewriteRule ^travel/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel2.php?cat=$1&subcat=$2

This is what I'm after from those last 3:

/travel/country/title-url-string (currently works & displays correct content)

/travel/category (currently works & displays correct content)

/travel/category/subcat (currently displays URL as I want it, but returns travel.php content instead of travel2.php)

I originally had an ID number in the travel.php rule, and removing that has resulted in the conflict. I'm aware two of the rules have the same pattern, so how can I best go about getting the results I want? Thanks for any help. :)

From last 3 rewrite rules:

1 & 3 check the same pattern where first one always take effect.

Further, as your category and country values cannot be distinguished each other here, I would suggest to append a unique identifier to the url as below:

RewriteRule ^travel/country-([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel.php?country=$1&url_string=$2
RewriteRule ^travel/category-([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ travel2.php?cat=$1&subcat=$2

So your matching urls would be:

/travel/country-countryname/title-url-string
/travel/category-categoryname/subcat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM