如何重写国家、州、城市的这些网址?

发布于 2024-12-31 23:59:35 字数 630 浏览 0 评论 0原文

我有一个网站,其中有来自世界各地提交的文章,我将它们的位置记录为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送舟行 2025-01-07 23:59:35

尝试编写较长条件优先的规则 -

RewriteRule ^page/(.*)/(.*)/(.*)/(.*)/(.*)$ page.php?site=$1&location=$2&countryname=$3&statename=$4&cityname=$5 [QSA]
RewriteRule ^page/(.*)/(.*)/(.*)/(.*)$ page.php?site=$1&location=$2&countryname=$3&statename=$4 [QSA]
RewriteRule ^page/(.*)/(.*)/(.*)$ page.php?site=$1&location=$2&countryname=$3 [QSA]

Try to write the rules where longer condition comes first -

RewriteRule ^page/(.*)/(.*)/(.*)/(.*)/(.*)$ page.php?site=$1&location=$2&countryname=$3&statename=$4&cityname=$5 [QSA]
RewriteRule ^page/(.*)/(.*)/(.*)/(.*)$ page.php?site=$1&location=$2&countryname=$3&statename=$4 [QSA]
RewriteRule ^page/(.*)/(.*)/(.*)$ page.php?site=$1&location=$2&countryname=$3 [QSA]
彼岸花似海 2025-01-07 23:59:35
  • 首先,确保每个斜杠之间没有斜杠 (/)。这是([^/]*)
  • 然后确保它决不为空。它是 + 而不是 *。所以,现在是 ([^/]+)
  • 然后,在验证规则后立即停止该规则:使用 L 指令。
  • 不要忘记让查询字符串通过 QSA 指令。

您的最终规则应该是这样的:

RewriteRule ^page/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ page.php?site=$1&location=$2&countryname=$3&statename=$4&cityname=$5 [QSA,L]
RewriteRule ^page/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ page.php?site=$1&location=$2&countryname=$3&statename=$4 [QSA,L]
RewriteRule ^page/([^/]+)/([^/]+)/([^/]+)$ page.php?site=$1&location=$2&countryname=$3 [QSA,L]

如果这还不够:

两个提示:

如果您不在托管环境中(= 如果它是您自己的服务器并且 >您可以修改虚拟主机,而不仅仅是.htaccess文件),尝试使用RewriteLog指令:它有帮助您可以追踪此类问题:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

我最喜欢的检查工具对于正则表达式:

http://www.quanetic.com/Regex (不要忘记选择 ereg( POSIX) 而不是 preg(PCRE)!)

  • First of all make sure there's no slash (/) between each slash. It's the ([^/]*).
  • Then make sure it's never empty. It's the + instead of the *. So, now, it's the ([^/]+).
  • Then stop immediately the rule once it has been validated: use the L directive.
  • Don't forget to make the query string follow via the QSA directive.

Here's what your final rules should be:

RewriteRule ^page/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ page.php?site=$1&location=$2&countryname=$3&statename=$4&cityname=$5 [QSA,L]
RewriteRule ^page/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ page.php?site=$1&location=$2&countryname=$3&statename=$4 [QSA,L]
RewriteRule ^page/([^/]+)/([^/]+)/([^/]+)$ page.php?site=$1&location=$2&countryname=$3 [QSA,L]

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 the RewriteLog directive: it helps you to track down such problems:

# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On

My favorite tool to check for regexp:

http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

温馨耳语 2025-01-07 23:59:35

好吧,我有办法做到这一点:

RewriteRule ^bylocation/([-a-z_]+)(?:/([-a-z_]+)(?:/([-a-z_]+))?)?/?$ location.php?country=$1&state=$2&city=$3 [NC,L,QSA]

这个单一的规则可以照顾,而不是三个规则。

Ok I have got a way to do that:

RewriteRule ^bylocation/([-a-z_]+)(?:/([-a-z_]+)(?:/([-a-z_]+))?)?/?$ location.php?country=$1&state=$2&city=$3 [NC,L,QSA]

This single rule can take care, instead of three rules.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文