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

时间:2022-04-03 11:03:53

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.

我需要将一些简短的便捷URL重定向到更长的实际URL。相关网站使用一组子域来标识一组开发或实时版本。

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.

我希望将某些请求重定向到的URL包含HTTP_HOST,这样我就不必为每个主机创建自定义.htaccess文件。

Host-specific Example (snipped from .htaccess file)

特定于主机的示例(从.htaccess文件中剪切)

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.

此示例适用于在dev01.example.com上运行的开发版本。如果我在主.htaccess文件中使用相同的行来运行dev02.example.com下的开发版本,我最终会被重定向到错误的位置。

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?
  • 这可以用mod_alias完成还是需要更复杂的mod_rewrite?

  • How can this be achieved using mod_alias or mod_rewrite? I'd prefer a mod_alias solution if possible.
  • 如何使用mod_alias或mod_rewrite实现这一目标?如果可能的话,我更喜欢mod_alias解决方案。

Clarifications

I'm not staying on the same server. I'd like:

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

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

  • https://secure.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/
  • 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/
  • 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.

我希望能够在example.com和dev.example.com上的.htaccess文件中使用相同的规则。在这种情况下,我需要能够将HTTP_HOST称为变量,而不是在重定向请求的URL中按字面指定它。

I'll investigate the HTTP_HOST parameter as suggested but was hoping for a working example.

我将根据建议调查HTTP_HOST参数,但希望有一个有效的例子。

7 个解决方案

#1


3  

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:

为了帮助您更快地完成工作,我最喜欢检查regexp的工具:

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

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

You use this tool when you want to check the URL and see if they're valid or not.

如果要检查URL并查看它们是否有效,可以使用此工具。

#2


0  

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

我想你会想要捕获HTTP_HOST值,然后在重写规则中使用它:

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

#3


0  

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重定向(告诉浏览器转到其他URL)。如果我的解决方案不适合您,请尝试使用此工具:http://www.htaccessredirect.net/index.php并找出适合您的方法。

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

//Change default directory page
DirectoryIndex 

#4


-1  

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

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

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.

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

#5


-1  

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

如果你停留在同一台服务器上,那么无论服务器如何,将它放在你的.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

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.

显然你需要调整REGEX匹配等,以确保它能够应对你将要投入的内容。同样适用于301,如果您不希望浏览器缓存重定向,则可能需要302。

If you want:

如果你想:

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

Then you'll need to use the HTTP_HOST parameter.

然后,您将需要使用HTTP_HOST参数。

#6


-2  

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

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

Redirect temp /terms /articles/terms/

This is explained in the mod_alias documentation:

mod_alias文档中对此进行了解释:

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.

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

#7


-2  

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

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

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

#1


3  

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:

为了帮助您更快地完成工作,我最喜欢检查regexp的工具:

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

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

You use this tool when you want to check the URL and see if they're valid or not.

如果要检查URL并查看它们是否有效,可以使用此工具。

#2


0  

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

我想你会想要捕获HTTP_HOST值,然后在重写规则中使用它:

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

#3


0  

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重定向(告诉浏览器转到其他URL)。如果我的解决方案不适合您,请尝试使用此工具:http://www.htaccessredirect.net/index.php并找出适合您的方法。

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

//Change default directory page
DirectoryIndex 

#4


-1  

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

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

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.

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

#5


-1  

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

如果你停留在同一台服务器上,那么无论服务器如何,将它放在你的.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

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.

显然你需要调整REGEX匹配等,以确保它能够应对你将要投入的内容。同样适用于301,如果您不希望浏览器缓存重定向,则可能需要302。

If you want:

如果你想:

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

Then you'll need to use the HTTP_HOST parameter.

然后,您将需要使用HTTP_HOST参数。

#6


-2  

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

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

Redirect temp /terms /articles/terms/

This is explained in the mod_alias documentation:

mod_alias文档中对此进行了解释:

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.

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

#7


-2  

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

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

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