如何重写国家、州、城市的这些网址?
我有一个网站,其中有来自世界各地提交的文章,我将它们的位置记录为 Country ->状态-> City
,为此我生成如下 URL:
site/location/countryname/statename/cityname
我有一个 php 文件,可以处理即将到来的请求并获取查询字符串中的值,现在的问题是如何编写 mod_rewrite 以便它可以具有像上面这样的 URL?
PS:如果有人删除了国家名称或国家名称,则不应传递空值,这意味着 URL 可以是:
site/location/countryname
site/location/countryname/statename
site/location/countryname/statename/cityname
但不能是:
site/location//statename/cityname
site/location/countryname//cityname
在上面的 url 中,如果国家名称被删除,则国家名称不应被视为国家名称。读取文件名为 location.php
谢谢,
I have a site where articles being submitted from worldwide and I record the location of them as Country -> State -> City
, for that I am generating URLs like:
site/location/countryname/statename/cityname
I have a single php file which can take care of coming request and gets values in query string, now the problem is how to write mod_rewrite so it can have URL like above?
PS: It should not pass empty values if someone deletes the statename or countryname, means URL can be:
site/location/countryname
site/location/countryname/statename
site/location/countryname/statename/cityname
But can not be:
site/location//statename/cityname
site/location/countryname//cityname
In above urls if countryname is deleted then statename shouldnt be treated as countryname. Reading file name is location.php
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试编写较长条件优先的规则 -
Try to write the rules where longer condition comes first -
/
)。这是([^/]*)
。+
而不是*
。所以,现在是([^/]+)
。L
指令。QSA
指令。您的最终规则应该是这样的:
如果这还不够:
两个提示:
如果您不在托管环境中(= 如果它是您自己的服务器并且 >您可以修改虚拟主机,而不仅仅是
.htaccess
文件),尝试使用RewriteLog
指令:它有帮助您可以追踪此类问题:我最喜欢的检查工具对于正则表达式:
http://www.quanetic.com/Regex (不要忘记选择 ereg( POSIX) 而不是 preg(PCRE)!)
/
) between each slash. It's the([^/]*)
.+
instead of the*
. So, now, it's the([^/]+)
.L
directive.QSA
directive.Here's what your final rules should be:
And if that's not enough:
Two hints:
If you're not in a hosted environment (= if it's your own server and you can modify the virtual hosts, not only the
.htaccess
files), try to use theRewriteLog
directive: it helps you to track down such problems:My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
好吧,我有办法做到这一点:
这个单一的规则可以照顾,而不是三个规则。
Ok I have got a way to do that:
This single rule can take care, instead of three rules.