如何使用 .htaccess 重定向到包含 HTTP_HOST 的 URL?
问题
我需要将一些简短的方便 URL 重定向到较长的实际 URL。 相关站点使用一组子域来标识一组开发或实时版本。
我希望某些请求重定向到的 URL 包含 HTTP_HOST,这样我就不必为每个主机创建自定义 .htaccess 文件。
特定于主机的示例(从 .htaccess 文件中截取)
Redirect /terms http://support.dev01.example.com/articles/terms/
此示例适用于在 dev01.example.com 上运行的开发版本。 如果我在 dev02.example.com 下运行的开发版本的主 .htaccess 文件中使用同一行,我最终会被重定向到错误的位置。
理想的规则(不确定语法是否正确)
Redirect /terms http://support.{HTTP_HOST}/articles/terms/
该规则不起作用,仅作为我想要实现的目标的示例。 然后我可以在许多不同的主机上使用完全相同的规则并获得正确的结果。
答案?
- 这可以用 mod_alias 来完成还是需要更复杂的 mod_rewrite?
- 如何使用 mod_alias 或 mod_rewrite 来实现这一点? 如果可能的话,我更喜欢 mod_alias 解决方案。
澄清
我不在同一台服务器上。 我想要:
- http://example.com/terms/ -> http://support。example.com/articles/terms/
- https://secure.example.com/terms/ -> http://support。example.com/articles/terms/
- http:// dev.example.com/terms/ -> http://support。dev.example.com/articles/terms/
- https://secure.dev.example.com/terms/ -> http://support。dev.example.com/articles/terms/
我想能够在 example.com 和 dev.example.com 上的 .htaccess 文件中使用相同的规则。 在这种情况下,我需要能够将 HTTP_HOST 作为变量引用,而不是在请求重定向到的 URL 中逐字指定它。
我将按照建议调查 HTTP_HOST 参数,但希望有一个可行的示例。
Problem
I need to redirect some short convenience URLs to longer actual URLs. The site in question uses a set of subdomains to identify a set of development or live versions.
I would like the URL to which certain requests are redirected to include the HTTP_HOST such that I don't have to create a custom .htaccess file for each host.
Host-specific Example (snipped from .htaccess file)
Redirect /terms http://support.dev01.example.com/articles/terms/
This example works fine for the development version running at dev01.example.com. If I use the same line in the main .htaccess file for the development version running under dev02.example.com I'd end up being redirected to the wrong place.
Ideal rule (not sure of the correct syntax)
Redirect /terms http://support.{HTTP_HOST}/articles/terms/
This rule does not work and merely serves as an example of what I'd like to achieve. I could then use the exact same rule under many different hosts and get the correct result.
Answers?
- Can this be done with mod_alias or does it require the more complex mod_rewrite?
- How can this be achieved using mod_alias or mod_rewrite? I'd prefer a mod_alias solution if possible.
Clarifications
I'm not staying on the same server. I'd like:
- http://example.com/terms/ -> http://support.example.com/articles/terms/
- https://secure.example.com/terms/ -> http://support.example.com/articles/terms/
- http://dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
- https://secure.dev.example.com/terms/ -> http://support.dev.example.com/articles/terms/
I'd like to be able to use the same rule in the .htaccess file on both example.com and dev.example.com. In this situation I'd need to be able to refer to the HTTP_HOST as a variable rather than specifying it literally in the URL to which requests are redirected.
I'll investigate the HTTP_HOST parameter as suggested but was hoping for a working example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您不需要包含此信息。 只需提供相对于根的 URI。
mod_alias 文档对此进行了解释:
You don't need to include this information. Just provide a URI relative to the root.
This is explained in the mod_alias documentation:
听起来您真正需要的只是一个别名?
It sounds like what you really need is just an alias?
如果您停留在同一服务器上,那么无论服务器如何,将其放入 .htaccess 中都将起作用:
Produces:
or:
显然,您需要调整 REGEX 匹配等,以确保它能够满足您的需求扔向它。 301 也是如此,如果您不希望浏览器缓存重定向,则可能需要 302。
如果您愿意:
那么您需要使用 HTTP_HOST 参数。
If you are staying on the same server then putting this in your .htaccess will work regardless of the server:
Produces:
or:
Obviously you'll need to adjust the REGEX matching and the like to make sure it copes with what you are going to throw at it. Same goes for the 301, you might want a 302 if you don't want browsers to cache the redirect.
If you want:
Then you'll need to use the HTTP_HOST parameter.
根据此备忘单( http://www.addedbytes.com/ download/mod_rewrite-cheat-sheet-v2/png/ )这应该可以工作
请注意,我没有办法测试它,所以这应该被视为正确方向的指针,而不是明确的答案。
According to this cheatsheet ( http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/ ) this should work
Note that i don't have a way to test this so this should be taken as a pointer in the right direction as opposed to an explicit answer.
如果我理解你的问题,你需要 301 重定向(告诉浏览器转到其他 URL)。
如果我的解决方案不适合您,请尝试以下工具: http://www.htaccessredirect.net/ index.php 并找出适合您的方法。
If I understand your question right, you want a 301 redirect (tell browser to go to other URL).
If my solution is not the correct one for you, try this tool: http://www.htaccessredirect.net/index.php and figure out what works for you.
我认为您需要捕获 HTTP_HOST 值,然后在重写规则中使用它:
I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:
奇怪的是,没有人完成实际的工作答案(笑):
为了帮助您更快地完成工作,我最喜欢的检查正则表达式的工具:
http://www.quanetic.com/Regex (不要忘记选择 ereg(POSIX) 而不是 preg(PCRE)!)
当您想要检查 URL 并查看它们是否有效。
It's strange that nobody has done the actual working answer (lol):
To help you doing the job faster, my favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)
You use this tool when you want to check the URL and see if they're valid or not.