用的.htaccess和重写规则问题重写、规则、问题、htaccess

2023-09-02 01:10:54 作者:氟西汀

我有网址的如游戏/的Xbox / 2 2是页码我需要的URL重写。这是我使用的是什么:

I have url's like games/xbox/2 2 being the page number I need the url rewritten. This is what I'm using:

RewriteRule games/(.*?)/$ games/consoles.php?console=$1 
RewriteRule games/(.+?)/(.+?)/$ games/consoles.php?console=$1&page=$2

第一条规则工作正常,但第二个是返回的Xbox consoles.php为$ 1,而不是

The first rule works fine but the second is returning consoles.php as $1 instead of xbox

推荐答案

试着这么做:

RewriteRule games/([^/]+)/([^/]+)/?$ games/consoles.php?console=$1&page=$2 [L]
RewriteRule games/([^/]+)/?$ games/consoles.php?console=$1 [L]

我第一次把你的最具体的规则先 - 这样,你不这样做一般的比赛,那么以后的更具体的比赛轧液,通用重写

I first put your most specific rule first - that way you don't do a general match, then a later more specific match mangles that general rewrite.

我还指定了 [L] 标志来表示要在发动机停止寻找在这一点上更多的比赛。重新排序规则是多余的在这种情况下,因为 [L] 标志,但它是一个很好的做法就可以进入。

I also specified the [L] flag to signify that you want the engine to stop looking for more matches at this point. Re-ordering the rules is redundant in this case because of the [L] flag, but it's a good practice to get into.

我也改变了前pressions咯。而不是使用([A-ZA-Z0-9] +)像previous海报说,我把它改为([^ /] +),因为这将匹配一切,但一个斜线,这样你就可以有怪异的控制台或游戏的名称。如果你想更具体的随意,但这种方式提供了最普通的用例。

I also changed the expressions slightly. Rather than using ([A-Za-z0-9]+) like the previous poster said, I changed it to ([^/]+) because that will match everything but a slash, so you can have weird console or game names. If you want to make it more specific feel free to, but this way provides the most general use-case.