mod_rewrite index.php将https重定向到http

时间:2016-05-11 14:38:43

标签: apache .htaccess redirect mod-rewrite ssl

我遇到mod_rewrite配置问题。我有一个网站,我需要通过index.php推送每个请求。我也想在每个网站上从http重定向到https,但有一个例外(文件夹/免费)。这听起来像是微不足道的任务,但我仍然有问题。我的.htaccess配置:

<IfModule mod_rewrite.c>
   SetEnv CAKEPHP_DEBUG 1
   RewriteEngine On

   RewriteCond %{HTTPS} on
   RewriteCond %{REQUEST_URI} ^/free$ [NC]
   RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

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

   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-l
   RewriteCond %{REQUEST_URI} !^/(img|css|js|shared)/(.*)
   RewriteRule ^(.*)$ index.php [QSA,L]
 </IfModule>

效果很好,除了/自由路径。调用https://test/free时,会转发到http。 http调用后,将返回重定向到https://website/index.php。这是错的 - 我想留在http / free文件夹中。我怎样才能正确地做到这一点?如果我从http注释到https,一切都运行良好。有任何想法吗?

编辑:来自重复链接的提案无效。问题是index.php重写的重复处理 - 很好的回答&#34;结束&#34;旗帜解决了我的问题

1 个答案:

答案 0 :(得分:2)

对于apache 2.3.9及更高版本:

如果您使用的是apache 2.3.9或更高版本,则可以使用上一个END中的RewriteRule标记:

RewriteRule ^(.*)$ index.php [QSA,END]

来自docs

  

立即停止重写过程,不再应用任何规则。还可以防止在每个目录和.htaccess上下文中进一步执行重写规则。 (2.3.9及更高版本中提供)

如果您不使用结束标记,则会重新检查您的请求,因此包含/index.php的重写请求与/free不同,并会从外部重定向到您的https页面。< / p>

对于2.3.9之前的apache:

如果您无法使用END标记,则必须使用包含完整HTTP请求行的%{THE_REQUEST},该行不会被内部重定向重写:

RewriteEngine on

RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} ^GET\ /free [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !^GET\ /free [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_URI} !^/(img|css|js|shared)/(.*)
RewriteRule .* index.php [QSA,L]
相关问题