Reputation: 863
I have a rewrite rule on apache2 in the .htaccess file that is working fine for the posts.php page.
mysite.com/post.php?id_name=this-is-the-title-of-my-site
becomes
mysite.com/this-is-the-title-of-my-site
I want to use the same rule for the cats.php page so that
mysite.com/cats.php?cat_label=articles
becomes
mysite.com/articles
I have used the same rule and changed the file name and the parameters but the rule for the cats.php page seems to fail and be overridden by the posts.php rule
I would prefer not to but I don't mind changing the structure to become
mysite.com/cats/articles
This is the .htaccess file that I am using at the moment
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id_name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ cats.php?cat_label=$1 [L,QSA]
All help is truly appreciated
Upvotes: 1
Views: 725
Reputation: 6469
The flag [L]
means "Last", as in, when that rule is matched, it is the last rewrite rule tested. You need a way to distinguish the two types of redirects. Here's two ways you might do that:
(1) Have dashes in all of your post names, but not in your categories, so both of your given examples work as given:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-]+)$ cats.php?cat_label=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id_name=$1 [L,QSA]
(2) Add a directory for the categories so that there isn't any magical character stuff going on, which can lead to mistakes. You already confronted this possibility above. Here's how it would look:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cats/(.*)$ cats.php?cat_label=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ post.php?id_name=$1 [L,QSA]
In both cases, we match the more specific versions first, flagging them as [L]
ast. Then we have the broad, catchall at the end.
Upvotes: 1