.htaccess:用查询字符串重写 URL

发布于 2024-11-08 01:38:44 字数 252 浏览 0 评论 0原文

我需要使用 .htaccess 重写 URL,并且该 URL 有两个查询字符串。这是我的原始 URL:

http://domain.com/channel-partners/en/index.php?location=phoenix-az&name=company

我如何将其写为:

http://cp.domain.com/phoenix-az/company

I need to rewrite a URL using an .htaccess and the URL has two query strings. Here is my raw URL:

http://domain.com/channel-partners/en/index.php?location=phoenix-az&name=company

How do I get that to be written as:

http://cp.domain.com/phoenix-az/company

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

倾听心声的旋律 2024-11-15 01:38:44
RewriteEngine On
RewriteBase /

RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ channel-partners/en/index.php?location=$1&name=$2 [L]

这会将 partners/location/name 格式的 URL(带有可选的尾部斜杠)重定向到您提供的原始 URL。 locationname 可以是字母数字、下划线或破折号字符。

注意:我已在 URL 中添加了文字 partners 前缀。如果没有它,您的 RewriteRule 将过于宽松并匹配几乎所有请求。请随意更改它。

更新

要从子域重定向,以上内容需要位于 cp.domain.com 的 .htaccess 文件中。您需要将 RewriteRule 修改为以下内容:

RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ http://domain.com/channel-partners/en/index.php?location=$1&name=$2 [L]

但是,我真的不建议这样做。链接管理将是一场噩梦。您确实应该尝试将所有这些链接放在同一域下。如果需要,可以从子域进行 301 重定向。

RewriteEngine On
RewriteBase /

RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ channel-partners/en/index.php?location=$1&name=$2 [L]

This will redirect URLs in the format partners/location/name with an optional trailing slash to the raw URL you provided. location and name can be alphanumeric, underscore, or dash characters.

Note: I have prefixed the URL with the literal partners. Without it your RewriteRule would be too loose and match nearly all requests. Feel free to change it.

UPDATE

To redirect from your sub-domain, the above needs to be in the .htaccess file for cp.domain.com. You'll need to modify your RewriteRule to the following:

RewriteRule ^partners/([\w-]+)/([\w-]+)/?$ http://domain.com/channel-partners/en/index.php?location=$1&name=$2 [L]

However, I really don't recommend this. Link management is going to be a nightmare. You should really try to have all these links under the same domain. 301 Redirect from the sub-domain if you have to.

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