在我的MVC应用程序中,我将使用.htaccess文件将所有请求定向到index.php文件。另外,我想强制执行SSL。使用this post,我添加了'#enforce https'代码。但是,当我将代码添加到.htaccess文件时,我的浏览器抱怨说它将无法完成请求。如何在同一文件中同时具有两个重写规则?
RewriteEngine On
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
#enforce https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
最佳答案
重写为index.php
的规则具有一个正则表达式:^(.+)$
,它匹配所有内容。这包括您要重定向的所有内容。在路由规则之前,您需要具有重定向规则。此外,您需要确保index.php中的所有内容都不会将浏览器重定向回非SSL:
RewriteEngine On
#enforce https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#redirect to index.php as appropriate
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
关于.htaccess - 如何结合两个.htaccess重定向?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31953601/