简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM