从WWW到根域的域重定向 - 工作但不完全正确 [英] Redirect from the domain with www to the root domain - working but not completely properly

查看:101
本文介绍了从WWW到根域的域重定向 - 工作但不完全正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要的是从 www.my_website123.com 重定向到 my_website123.com 任何网址 www.my_website123.com/some_thing my_website123.com/some_thing 。我有这个文件中的 /etc/apache2/sites-available/sub_domain123.main_domain123.com

 <虚拟主机*:80>
  服务器名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 /公
  DocumentRoot的/web/sub_domain123.my_website123.com/current/public
  错误日志/var/log/apache2/sub_domain123_errors.log
  LOGLEVEL警告
  的CustomLog /var/log/apache2/sub_domain123_access.log结合
  SETENV生产RAILS_ENV
  头中设置访问控制,允许原产地http://sub_domain456.my_website123.com
  头设置访问控制允许的凭据真
  RewriteEngine叙述上
  的RewriteCond%{HTTP_HOST} ^ WWW \\ .my_website123 \\ .COM $ [NC]
  重写规则^(。*)$ http://my_website123.com/$1 [R = 301,L]  <目录/ var / sub_domain123 /公共>
    所有允许
    选项​​-MultiViews
  < /目录>
< /虚拟主机><虚拟主机*:443>
 .....

其中,

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST} ^ WWW \\ .my_website123 \\ .COM $ [NC]
重写规则^(。*)$ http://my_website123.com/$1 [R = 301,L]

是我加的。然后,我这样做:

  a2enmod重写
/etc/init.d/apache2重启

它几乎正常工作。但是:

 卷曲-I http://www.my_website123.com
HTTP / 1.1 302找到
日期:孙老师,2014年12月28日4时12分04秒GMT
服务器:Apache / 2.2.22(Ubuntu的)
的X已启动方式:Phusion的客运(mod_rails / mod_rack)3.0.21
缓存控制:无缓存


  1. 请我不得不删除 ServerAlias​​ www.my_website123.com

  2. 请我一定要重命名服务器名称my_website123.com 服务器名称www.my_website123.com

  3. 它重定向我www.my_website123.com/login到 http://my_website123.com//login 这是OK,除了它包含双斜线的事实( .COM //登录)。我该如何去除呢?

  4. 为什么会返回302状态code,而不是301?


解决方案

您需要删除结尾的斜线 / 在重写规则(的参考)。此外,需要一个服务器和ServerAlias​​,因为它是。因此,更新的规则看起来像下面。

  RewriteEngine叙述上
的RewriteCond%{HTTP_HOST} ^ WWW \\ .my_website123 \\ .COM $ [NC]
重写规则^(。*)$ HTTP://my_website123.com$1 [R = 301,L]

不知道为什么要返回 302 。但是,尝试更新的规则,看看你会得到什么回应。

祝您好运!

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. I have this in the file /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 ?
  2. Do I have to rename ServerName my_website123.com to be 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). How do I remove it?
  4. Why does it return 302 status code instead of 301?

解决方案

You will need to remove a trailing slash / in RewriteRule (Reference). Also, need a ServerName and ServerAlias as it is. 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. But, try with the updated rule and see what response you get.

Good Luck!

这篇关于从WWW到根域的域重定向 - 工作但不完全正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆