Search code examples
.htaccess

apply RewriteRule only if string exists in url


I have Urls which will always have http://www.example.com/global/ but after /global/ there may be name of a city or franchise in city or some other page

Examples are http://www.example.com/global/name-of-city or http://www.example.com/global/frnachise-then-name-of-city

Now I have written some rules for both of the conditions & they are working

This is what I'm doing for urls like http://www.example.com/global/name-of-city

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f    
RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]

And here is what I'm doing for franchise including urls

RewriteRule ^franchise-management-.([\w-]+)/?$ franchise.php?city=$1 [L,QSA]

but the issue is the 2nd one works only if the 1st RewriteRule is commented otherwise 1st will be applied always

I need to put some condition with a check if url includes franchise then execute RewriteRule for franchise only otherwise execute the 1st RewriteRule.


Solution

  • Try :

    RewriteCond %{REQUEST_FILENAME} -d [OR]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]   
    RewriteRule ^franchise-management/([\w-]+)/?$ franchise-management.php?city=$1 [L,QSA]
    RewriteRule ^([\w-]+)/?$ products.php?city=$1 [L,QSA]