在Apache上将HTTP重定向到https和www重定向到非www

时间:2019-04-15 13:57:30

标签: apache .htaccess redirect

我需要基于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>

1 个答案:

答案 0 :(得分:0)

尝试将规则更改为:

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

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