Q:

.htaccess contineu if redirect rule was not met

I am create a site that I want to rewrite the subdirectory but it could be two possible outcomes.
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ pages/$2.php?state=$1 [NC,L]

RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index_agent.php?state=$1&city=$2 [NC,L]

The page can either be site.com/state/contact-us or site.com/state/city, with it checking the first rule first. The code above works on the first rule if the page exist, but if the page doesn't exits it doesn't move on to the second rule.

A:

For the first rule check if corresponding .php files exists:
RewriteCond %{DOCUMENT_ROOT}/pages/$2.php -f
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ pages/$2.php?state=$1 [QSA,L,NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/?$ index_agent.php?state=$1&city=$2 [NC,QSA,L]

