Matej Golian
Matej Golian

Reputation: 55

Multiple Rewrite Rules in a .htaccess File

I'm having problems with url rewriting.

Here's how my .htaccess file looks like:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^blog\/$ /blog.php [L]
RewriteRule ^blog\/[a-zA-Z0-9\-]+\/([0-9]+)-([0-9]+)\/$ /blog.php?action=listposts&categoryid=$1&page=$2 [L]
RewriteRule ^blog\/featured\/([0-9]+)\/$ /blog.php?action=listposts&categoryid=featured&page=$1 [L]
RewriteRule ^blog\/[a-zA-Z0-9\-]+\/([0-9]+)\/[a-zA-Z0-9\-]+\/$ /blog.php?action=viewpost&postid=$1 [L]
</IfModule>

I don't know why, but it doesn't work. For example, when I go to a url like blog/my-category/1-5/ and print out $_GET, it has no keys, as if I was on the url blog/. Sorry for this question as it most likely is a duplicate, but I really don't know what to do and I guess I wouldn't understand what the problem is based on a solution to somebody else's question.

Upvotes: 0

Views: 342

Answers (1)

AbsoluteƵER&#216;
AbsoluteƵER&#216;

Reputation: 7880

Rules are processed in the order in which they're encountered (top down). Also you're escaping a few things that do not need to be escaped. - Hyphen characters inside of brackets [] at the end do not need to be escaped. If they're between things they're treated as ranges (eg. [0-9]). The slashes / do not need to be escaped, but sometimes the trailing slash (depending on your settings) may not be required, so I've added a ? character to make them not always required.

RewriteEngine on
RewriteBase /

RewriteRule ^blog/[a-zA-Z0-9-]+/([0-9]+)-([0-9]+)/?$ /blog.php?action=listposts&categoryid=$1&page=$2 [L]
RewriteRule ^blog/featured/([0-9]+)/?$ /blog.php?action=listposts&categoryid=featured&page=$1 [L]
RewriteRule ^blog/[a-zA-Z0-9-]+/([0-9]+)/[a-zA-Z0-9-]+/?$ /blog.php?action=viewpost&postid=$1 [L]
RewriteRule ^blog/?$ /blog.php [L]

If you've used the preg_ functions in PHP that might be where your slash escaping habit is coming from as a lot of online examples and books use the / characters as delimiters.

Upvotes: 1

Related Questions