.htaccess:用查询字符串重写 URL
我需要使用 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将
partners/location/name
格式的 URL(带有可选的尾部斜杠)重定向到您提供的原始 URL。location
和name
可以是字母数字、下划线或破折号字符。注意:我已在 URL 中添加了文字
partners
前缀。如果没有它,您的 RewriteRule 将过于宽松并匹配几乎所有请求。请随意更改它。更新
要从子域重定向,以上内容需要位于 cp.domain.com 的 .htaccess 文件中。您需要将 RewriteRule 修改为以下内容:
但是,我真的不建议这样做。链接管理将是一场噩梦。您确实应该尝试将所有这些链接放在同一域下。如果需要,可以从子域进行 301 重定向。
This will redirect URLs in the format
partners/location/name
with an optional trailing slash to the raw URL you provided.location
andname
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:
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.