用于 SSL 重定向的重写引擎



我的.htaccess的相关设置粘贴在下面。效果很好,除了...如果我输入 https://mydomain/abc.php,它不会重定向到 https://mydomain/www/abc.php。另一方面,如果我键入 http://mydomain/abc.php,它会正确重定向到 https://mydomain/www/abc.php。我希望两者都重定向到 https://mydomain/www/abc.php。您对正则表达式(我的盲点(进行必要调整的帮助将不胜感激。

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

如果您使用的是 apache,则不推荐使用 .htaccess 文件进行重定向。(你可以看看这里。他们建议直接在主服务器文件中使用 Redirect 指令。你可以这样:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect / https://www.example.com/www/
</VirtualHost>
<VirtualHost *:443>
    ServerName www.example.com
    Redirect / https://www.example.com/www/
    # ... SSL configuration goes here
</VirtualHost>

如果您无法直接访问服务器,您可以在 .htaccess 文件中执行此操作,如下所示:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI}/ www
RewriteRule ^(.*) %{HTTP_HOST}%/www/%{REQUEST_URI} [R=302,NC]
RewriteRule ^(.*) %{HTTP_HOST}%/www/%{REQUEST_URI} [R=302,NC]

最新更新