【问题标题】:mod_rewrite index.php redirect https to httpmod_rewrite index.php 将 https 重定向到 http
【发布时间】:2016-05-11 14:38:42
【问题描述】:

我的 mod_rewrite 配置有问题。我有一个网站,我需要通过 index.php 推送每个请求。我还想在每个网站上从 http 重定向到 https,但有一个例外(文件夹 /free)。这听起来像微不足道的任务,但我仍然有问题。我的 .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>

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

编辑:来自重复链接的提案无效。问题是 index.php 重写的重复处理 - 带有“end”标志的好答案解决了我的问题

【问题讨论】:

标签: apache .htaccess redirect mod-rewrite ssl


【解决方案1】:

对于 apache 2.3.9 及更高版本:

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

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

来自docs:

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

如果您不使用结束标志,您的请求将被重新检查,因此包含/index.php 的重写请求与/free 不同,并将被外部重定向到您的https页面。

对于 2.3.9 之前的 apache:

如果您不能使用END 标志,则必须使用%{THE_REQUEST},其中包含未被内部重定向重写的完整HTTP 请求行:

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]

【讨论】:

    猜你喜欢
    • 2013-07-05
    • 1970-01-01
    • 2018-04-15
    • 2018-04-03
    • 1970-01-01
    • 2020-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多