简体   繁体   English

从具有www的域重定向到根域-工作但不完全正确

[英]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.commy_website123.com任何URL www.my_website123.com/some_thingmy_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
  1. Do I have to delete ServerAlias www.my_website123.com ? 我是否必须删除ServerAlias www.my_website123.com
  2. Do I have to rename ServerName my_website123.com to be ServerName www.my_website123.com 我是否必须将ServerName my_website123.com重命名为ServerName www.my_website123.com
  3. It redirects me www.my_website123.com/login to http://my_website123.com//login which is OK except the fact that it contains the double slash ( .com//login ). 它将我www.my_website123.com/login重定向到http://my_website123.com//login ,除了它包含双斜杠( .com//login )之外,其他都可以。 How do I remove it? 如何删除它?
  4. Why does it return 302 status code instead of 301? 为什么返回302状态代码而不是301?

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM