casusbelli casusbelli - 1 month ago 12
PHP Question

RewriteEngine messing up the first RewriteConds after adding a new one

I'm pretty new with url-rewriting and I run into an issue today where I can't find a solution to solve.

Here is my .htaccess RewriteConds which is working

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_URI} models/([a-z\-A-Z]+)/([a-z\-A-Z]+)
RewriteRule (.*) p.php?model=%1&media=%2 [L]

RewriteCond %{REQUEST_URI} models/([a-z\-A-Z]+)
RewriteRule (.*) orderby.php?model=%1 [L]


However when I add a new RewriteCond to redirect models.php to website.com/models. It works but the RewriteCond bellow do not work anymore.

RewriteCond %{REQUEST_URI} models
RewriteRule (.*) models.php [L]


Anyone have a idea of what I'am doing wrong?

Answer

You need to use anchors in your regex to avoid matching unwanted part:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_URI} /models/([a-z\-A-Z]+)/([a-z\-A-Z]+)/?$ [NC]
RewriteRule . p.php?model=%1&media=%2 [L,QSA]

RewriteCond %{REQUEST_URI} /models/([a-z\-A-Z]+)/?$ [NC]
RewriteRule . orderby.php?model=%1 [L,QSA]

RewriteCond %{REQUEST_URI} /models/?$ [NC]
RewriteRule . models.php [L]
Comments