【问题标题】:Unable to get rid of www prefix in apache无法摆脱 apache 中的 www 前缀
【发布时间】:2013-08-23 23:39:09
【问题描述】:

我正在尝试在 Apache (v2.2.22 - Debian) 中重写所有以下 url:

http://mydomain.com/something

http://www.mydomain.com/something

https://www.mydomain.com/something

https://mydomain.com/something

将 http 重写为 https 没问题 - 这工作正常。但我似乎无法摆脱 www 前缀。我的域确实使用 www 运行,但是我的 SSL 证书恰好只对 mydomain.com 和 mail.mydomain.com 有效,而对 www.mydomain.com 无效 - 所以 www 在我的浏览器中给我 SSL 警告。

我在网上尝试了很多建议,但似乎都不起作用。目前在我的 /etc/apache2/sites-available/default (由 apache2.conf 提取)如下:

RewriteEngine On
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://mydomain.com/$1 [L]

RewriteCond %{HTTP_HOST}  ^www\. [NC]
RewriteRule ^(.*)$ https://mydomain.com/$1 [L]

正如我所说,这适用于 http => https,而不是丢失 www 前缀。有什么想法吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    您忘记执行外部重定向。添加[R=301] 标志。

    RewriteEngine On
    
    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST}  ^www\. [NC]
    RewriteRule ^(.*)$ https://mydomain.com/$1 [R=301,L]
    

    【讨论】:

    • 谢谢,但似乎没有帮助。
    • @planck 两条规则都需要 301。
    猜你喜欢
    • 2020-06-01
    • 2015-11-23
    • 2011-07-06
    • 2017-06-14
    • 1970-01-01
    • 2021-04-29
    • 1970-01-01
    • 2012-10-14
    • 1970-01-01
    相关资源
    最近更新 更多