开发者

mod_rewrite conflict with other $_GET vars

I am using mod_rewrite to make my URLs clean. By doing so:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&sub=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&sub=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)$ index.php?page=$1&sub=$2&id=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([0-9]+)/$ index.php?page=$1&sub=$2&id=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&sub=$2&action=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&sub=$2&action=$3

This changes everything nicely from for example: website.com/index.php?page=user&sub=profile to website.com/user/profile/.

But what if there are other $_GET variables AFT开发者_运维问答ER profile. So for example, if the user calls for: website.com/user/profile/?do=that&go=ahead

When I try to print $_GET['do'] and $_GET['go'], they return empty.

Any ideas?

Suggestions to make my mod_rewrite code shorter are also welcome :)


In that case, you need to add the QSA flag. Furthermore, I'd say that all your 6 rules are basically doing the same. You can add optional sections to your regular expressions with the ? operator. For instance, these lines:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?page=$1&sub=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?page=$1&sub=$2

... can be merged as:

RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ index.php?page=$1&sub=$2 [QSA]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜