如何使用 .htaccess 重定向到包含 HTTP_HOST 的 URL?

发布于 2024-07-05 01:04:51 字数 1878 浏览 17 评论 0原文

问题

我需要将一些简短的方便 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 解决方案。

澄清

我不在同一台服务器上。 我想要:

我想能够在 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:

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 技术交流群。

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

发布评论

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

评论(7

绝對不後悔。 2024-07-12 01:04:51

您不需要包含此信息。 只需提供相对于根的 URI。

Redirect temp /terms /articles/terms/

mod_alias 文档对此进行了解释:

新 URL 应该是以方案和主机名开头的绝对 URL,但也可以使用以斜线开头的 URL 路径,在这种情况下,将添加当前服务器的方案和主机名。

You don't need to include this information. Just provide a URI relative to the root.

Redirect temp /terms /articles/terms/

This is explained in the mod_alias documentation:

The new URL should be an absolute URL beginning with a scheme and hostname, but a URL-path beginning with a slash may also be used, in which case the scheme and hostname of the current server will be added.

画▽骨i 2024-07-12 01:04:51

听起来您真正需要的只是一个别名?

Alias /terms /www/public/articles/terms/

It sounds like what you really need is just an alias?

Alias /terms /www/public/articles/terms/
久隐师 2024-07-12 01:04:51

如果您停留在同一服务器上,那么无论服务器如何,将其放入 .htaccess 中都将起作用:

RedirectMatch 301 ^/terms$ /articles/terms/

Produces:

http://example.com/terms -> http://example.com/articles/terms

or:

http://test.example.com/terms -> http://test.example.com/articles/terms

显然,您需要调整 REGEX 匹配等,以确保它能够满足您的需求扔向它。 301 也是如此,如果您不希望浏览器缓存重定向,则可能需要 302。

如果您愿意:

http://example.com/terms -> http://server02.example.com/articles/terms

那么您需要使用 HTTP_HOST 参数。

If you are staying on the same server then putting this in your .htaccess will work regardless of the server:

RedirectMatch 301 ^/terms$ /articles/terms/

Produces:

http://example.com/terms -> http://example.com/articles/terms

or:

http://test.example.com/terms -> http://test.example.com/articles/terms

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:

http://example.com/terms -> http://server02.example.com/articles/terms

Then you'll need to use the HTTP_HOST parameter.

如日中天 2024-07-12 01:04:51

根据此备忘单( http://www.addedbytes.com/ download/mod_rewrite-cheat-sheet-v2/png/ )这应该可以工作

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1

请注意,我没有办法测试它,所以这应该被视为正确方向的指针,而不是明确的答案。

According to this cheatsheet ( http://www.addedbytes.com/download/mod_rewrite-cheat-sheet-v2/png/ ) this should work

RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1

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.

无名指的心愿 2024-07-12 01:04:51

如果我理解你的问题,你需要 301 重定向(告诉浏览器转到其他 URL)。
如果我的解决方案不适合您,请尝试以下工具: http://www.htaccessredirect.net/ index.php 并找出适合您的方法。

//301 Redirect Entire Directory
RedirectMatch 301 /terms(.*) /articles/terms/$1

//Change default directory page
DirectoryIndex 

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.

//301 Redirect Entire Directory
RedirectMatch 301 /terms(.*) /articles/terms/$1

//Change default directory page
DirectoryIndex 
段念尘 2024-07-12 01:04:51

我认为您需要捕获 HTTP_HOST 值,然后在重写规则中使用它:

RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/terms http://support.%1/article/terms [NC,R=302]

I think you'll want to capture the HTTP_HOST value and then use that in the rewrite rule:

RewriteCond %{HTTP_HOST} (.*)
RewriteRule ^/terms http://support.%1/article/terms [NC,R=302]
ˇ宁静的妩媚 2024-07-12 01:04:51

奇怪的是,没有人完成实际的工作答案(笑):

RewriteCond %{HTTP_HOST} support\.(([^\.]+))\.example\.com
RewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]

为了帮助您更快地完成工作,我最喜欢的检查正则表达式的工具:

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

当您想要检查 URL 并查看它们是否有效。

It's strange that nobody has done the actual working answer (lol):

RewriteCond %{HTTP_HOST} support\.(([^\.]+))\.example\.com
RewriteRule ^/terms http://support.%1/article/terms [NC,QSA,R]

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.

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