.htaccess 301重定向无法正常工作.需要强制HTTPS

Gig*_*igi 2 apache .htaccess mod-rewrite https redirect

我们希望强制我们网站的所有请求都使用HTTPS协议.我们只想替换URL的协议,其余的URI可以保持不变.当我们从主页开始浏览网站时,一切正常.当我们首先打开不是主页的任何其他页面(即ourdomain.com/this-is-a-page/)时,我们不会被重定向到使用HTTPS.我需要在htaccess文件上进行哪些更改才能完成此操作?

这有效(它插入https):ourdomain.com

这不起作用:ourdomain.com/this-is-a-page/

谢谢!

htaccess代码:

RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Run Code Online (Sandbox Code Playgroud)

Jon*_*Lin 5

您需要路由规则之前放置重定向规则.重写引擎循环,所以首先执行路由规则(路由东西index.php),重写引擎循环,然后应用第二个重定向规则,重定向错误的东西.尝试:

RewriteEngine On
RewriteBase /

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

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Run Code Online (Sandbox Code Playgroud)