.htaccess中重写器中的模式干扰 [英] Pattern interference in rewriterules in .htaccess

查看:46
本文介绍了.htaccess中重写器中的模式干扰的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在.htaccess中定义重写器时,我遇到模式干扰的麻烦.

I have trouble with a pattern interference when defining rewriterules in .htaccess.

我要重写的网站链接是:

The website links I am trying to rewrite are:

  • example.com/?item=work-> example.com/work
  • example.com/?item=work&tile=x-> example.com/work/x
  • example.com/?item=travel-> example.com/travel
  • example.com/?item=travel&blog=x-> example.com/travel/x
  • example.com/?item=about-> example.com/about

我使用了在线重写规则生成器,因为我非常了解在这里限制.对于"tile"和"blog" URL的重写规则,我分别在模式中添加了"work"和"travel"以使其唯一.对于常规页面"work","travel"和"about",我无法应用相同的逻辑,因为斜杠前没有任何内容(我可以以某种方式包括完整的URL吗?).因此,它会干扰其他两个规则:

I have used the online rewrite rule generator as my knowledge is very limited here. For the rewrite rules for the "tile" and "blog" URL I have added the "work" and "travel" respectively in the pattern to make them unique. For the general pages "work","travel" and "about" I cannot apply the same logic as there is nothing left before the slash (can I somehow include the full URL as a pattern?). Thus, it interferes with the other two rules:

RewriteEngine On
RewriteRule ^([^/]*)$ /?item=$1 [L]
RewriteRule ^travel/([^/]*)$ ?item=travel&blog=$1 [L]
RewriteRule ^work/([^/]*)$ /?item=work&tile=$1 [L]

我尝试做一些研究,发现我可以包括一个附加条件来指定它是一个项目:

I have tried to do some research and found that I can include an additional criteria to specify it is an item:

RewriteRule ^item/([^/]*)$ /?item=$1 [L]

但是还有一种方法可以不需要编写规则,使用"example.com/about"会将您重定向到"example.com/?item=about",而无需会干扰其他两个规则吗?

But is there also a way to write the rule(s) in such a way that this is not needed and using "example.com/about" will simply redirect you to "example.com/?item=about" without interfering with the other two rules?

修改

我只是想在洗完澡后就可以将工作",关于"和旅行"本身用作模式.这将产生以下规则集,并且它们会实际起作用(如果它们以正确的顺序排列):-)

I was just thinking under the shower that I can use "work", "about" and "travel" as patterns themselves. This results in the below set of rules and they actually work (if they are in the right order) :-)

RewriteRule ^travel/([^/]*)$ ?item=travel&blog=$1 [L]
RewriteRule ^work/([^/]*)$ /?item=work&tile=$1 [L]
RewriteRule ^about /?item=about [L]
RewriteRule ^travel /?item=travel [L]
RewriteRule ^work /?item=work [L]

但是,这看起来很脏.有更好的方法吗?

However, this looks quite dirty. Is there a better way to do this?

推荐答案

看起来不错.为了改进它,可以将后三个替换为:

Looks good. To improve it, the last three can be replaced with:

RewriteRule ^(about|travel|work)$ /?item=$1 [L]

其他两个需要保持不变,因为参数的命名不同,因此不能在一条规则中完成.

The other two need to stay the same because the parameters are named differently so can't be done in one rule.

这篇关于.htaccess中重写器中的模式干扰的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆