【问题标题】:Redirect domain https://domain.com to https://www.domain.com without rewrite rules just redirect in apache将域 https://domain.com 重定向到 https://www.domain.com 而无需重写规则,只需在 apache 中重定向
【发布时间】:2015-10-24 12:00:27
【问题描述】:

我正在尝试从 https://domain.com 重定向到 https://www.domain.com

我可以通过重写实现以下任务,但只希望通过重定向 301 实现。

VirtualHost domain.com:80 
RewriteEngine on 
ReWriteCond %{SERVER_PORT} !^443$ 
RewriteRule ^/?(.*) https://%{HTTP_HOST}/$1 [NC,R,L] 
VirtualHost _default_:443 
ServerAdmin webmaster@localhost 
ServerName domain.com:443 
ServerAlias www.domain.com 
DocumentRoot /var/www/html 
RewriteEngine on 
RewriteCond %{HTTP_HOST} !^(www\.)?(.+) 
RewriteRule ^ https://%2%{REQUEST_URI} [R=301,L]

重定向对我不起作用,因为我试图将 / 重定向到 https:// 但它重定向到 https://domain.com 而不是 https://www.domain.com

【问题讨论】:

  • 你试过了吗:RewriteRule ^ https://www.%2%{REQUEST_URI} [R=301,L]
  • @Leogout :我不想使用重写,我只想尽可能通过重定向来实现。
  • @user3355434 你不想使用重写的原因是什么?您不能使用 Redirect 来做您想做的事情,因为它不需要条件。 httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect我在回答中提到过。

标签: apache .htaccess mod-rewrite rewrite http-redirect


【解决方案1】:

您不应为此使用mod-alias (Redirect)Rewrite 更强大、更简单,也是最常见的基于条件的重定向方式。重定向将处理目录但没有任何条件。也许您应该以这种方式尝试重写规则。使用 Rewrite,您正在执行 301,这就是标志 R=301 的含义。

RewriteEngine on 
RewriteCond %{HTTP_HOST} !^(www\.)
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

【讨论】:

  • 我认为 app.hostname 之类的所有内容也会转到 www.hostname?
猜你喜欢
  • 1970-01-01
  • 2013-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-22
  • 1970-01-01
  • 2016-10-17
相关资源
最近更新 更多