[英]Redirect from the domain with www to the root domain - working but not completely properly
What I need is to redirect from www.my_website123.com
to my_website123.com
for any url www.my_website123.com/some_thing
to my_website123.com/some_thing
. 我需要的是从重定向
www.my_website123.com
到my_website123.com
任何URL www.my_website123.com/some_thing
到my_website123.com/some_thing
。 I have this in the file /etc/apache2/sites-available/sub_domain123.main_domain123.com
我在文件
/etc/apache2/sites-available/sub_domain123.main_domain123.com
有此文件
<VirtualHost *:80>
ServerName my_website123.com
ServerAlias www.my_website123.com
ServerAlias sub_domain123.my_website123.com
ServerAlias files.my_website123.com
ServerAdmin admin@my_website123.com
#DocumentRoot /var/sub_domain123/public
DocumentRoot /web/sub_domain123.my_website123.com/current/public
ErrorLog /var/log/apache2/sub_domain123_errors.log
LogLevel warn
CustomLog /var/log/apache2/sub_domain123_access.log combined
SetEnv RAILS_ENV production
Header set Access-Control-Allow-Origin "http://sub_domain456.my_website123.com"
Header set Access-Control-Allow-Credentials true
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.my_website123\.com$ [NC]
RewriteRule ^(.*)$ http://my_website123.com/$1 [R=301,L]
<Directory /var/sub_domain123/public>
Allow from all
Options -MultiViews
</Directory>
</VirtualHost>
<VirtualHost *:443>
.....
Where 哪里
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.my_website123\.com$ [NC]
RewriteRule ^(.*)$ http://my_website123.com/$1 [R=301,L]
is what I added. 是我添加的。 Then I did this:
然后我这样做:
a2enmod rewrite
/etc/init.d/apache2 restart
It almost works properly. 它几乎可以正常工作。 However:
然而:
curl -I http://www.my_website123.com
HTTP/1.1 302 Found
Date: Sun, 28 Dec 2014 04:12:04 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: Phusion Passenger (mod_rails/mod_rack) 3.0.21
Cache-Control: no-cache
ServerAlias www.my_website123.com
? ServerAlias www.my_website123.com
? ServerName my_website123.com
to be ServerName www.my_website123.com
ServerName my_website123.com
重命名为ServerName www.my_website123.com
.com//login
). .com//login
)之外,其他都可以。 How do I remove it? You will need to remove a trailing slash /
in RewriteRule ( Reference ). 您将需要在RewriteRule( 参考 )中删除结尾的斜杠
/
。 Also, need a ServerName and ServerAlias as it is. 另外,需要一个ServerName和ServerAlias。 So, updated rule will look like below.
因此,更新后的规则如下所示。
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.my_website123\.com$ [NC]
RewriteRule ^(.*)$ http://my_website123.com$1 [R=301,L]
Not sure why should it return 302
. 不知道为什么要返回
302
。 But, try with the updated rule and see what response you get. 但是,请尝试使用更新后的规则,看看会得到什么响应。
Good Luck! 祝好运!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.