【问题标题】:redirecting http to https and www to non www on apache将http重定向到https和www到apache上的非www
【发布时间】:2025-11-25 02:45:02
【问题描述】:

我需要在我们的 apache2 和站点 .htaccess 上(希望)设置高级(对我而言)重定向。 我们希望任何指向 www.example.com 和 example.com 或 https://www.example.com 的人都将被重定向到 https://example.com

问题是域后的链接必须有效。 例如,http://example.com/category/article/newesthttps://www.example.com/category/article/newest 将在重定向后工作并指向正确的 https://example.com/category/article/newest

我们尝试了重定向,如您在附加的 .htaccess 中所见,但问题是它例如将 http://www.example.com/category/article/newest 重定向到 https://example.com/

我们当前的 .htaccess

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.$ [NC]
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule .* public/index.php
SetEnv APPLICATION_ENV development
<IfModule mod_php.c>
    php_flag magic_quotes_gpc off
    php_flag register_globals off
</IfModule>

【问题讨论】:

    标签: apache .htaccess redirect


    【解决方案1】:

    尝试将您的规则更改为:

    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\.$ [NC]
    RewriteRule .* https://example.com%{REQUEST_URI} [R=301,L]
    

    这样,如果请求不是 HTTPS如果请求以“www”开头,它将被重定向到 HTTPS 而没有“www”。

    【讨论】:

      最近更新 更多