如何使%{HTTP_HOST}始终返回非www

时间:2012-04-24 12:54:09

标签: apache .htaccess mod-rewrite

我有这个重定向代码,我希望无论使用什么子域,我总是将%{HTTP_HOST}作为非www返回。

RewriteEngine On
RewriteRule ^.*$ http://www.mywebsite.com/%{HTTP_HOST} [L,R=301]

因此,www.website2.com,website2.com,anything.website2.com将始终重定向至http://www.mywebsite.com/website2.com

感谢您的时间。

3 个答案:

答案 0 :(得分:0)

我会使用Google,然后点击第一个链接。
http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/

然后你可以改变RewriteCond以匹配任何东西,而不是特别不是-www

答案 1 :(得分:0)

您可以尝试这样的事情:

# we are now working on HTTP_REFERER,
# change to appropriate variable according to your needs
#   check if we got at least 2 dots in the URL
RewriteCond %{HTTP_REFERER} \.([^\.]+)\.([A-Z]{2,3})$ [NC]
#   if the condition was met, redirect using only last 2 parts of URL
RewriteRule ^ http://www.mywebsite.com/%1.%2 [R,L]

如果这有用,请告诉我。

答案 2 :(得分:0)

让你的重写规则如下:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(?:[^.]+\.)(.+)$ [NC]
RewriteRule ^ http://www.mywebsite.com/%1%{REQUEST_URI} [L,R]