【问题标题】:Exclude a URL from mod_rewrite rule - stop redirect to home page从 mod_rewrite 规则中排除 URL - 停止重定向到主页
【发布时间】:2018-01-06 10:57:21
【问题描述】:

我需要一个 mod_rewrite 将所有 http 请求重定向到 https,但我想排除一个 URL

这是我目前在 .htaccess 文件中的内容。

# force https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/template/.*$ [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

http://example.com/template/` 不应被重定向。但它正在被重定向到主页。

http://example.com/template/
301 Moved Permanently
https://example.com/index.php
301 Moved Permanently
https://example.com/
200 OK

谁能弄清楚为什么它会导致重定向到主页?

谢谢!

【问题讨论】:

标签: wordpress apache .htaccess mod-rewrite


【解决方案1】:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /template/.*\ HTTP [NC]
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,QSA]

.htaccess 不好。第一个请求http://example.com/template/ 匹配最后一个RewriteRule,它在内部重写为http://example.com/index.php,它匹配第一个RewriteRule,并永久重定向到https://example.com/index.php。 REQUEST_URI 在内部被重写,因为之前的 RewriteRule 引用了它。 THE_REQUEST 是最初从客户端收到的确切请求标头。它不会因任何内部重写而更新。

REQUEST_URI

请求的 URI 的路径组件,例如 “/index.html”。这特别排除了查询字符串,它是 可作为其自己的名为 QUERY_STRING 的变量使用。

THE_REQUEST

浏览器向服务器发送的完整 HTTP 请求行(例如,“GET /index.html HTTP/1.1")。这不包括任何额外的标头 由浏览器发送。这个值没有被转义(解码), 与下面的大多数其他变量不同。

【讨论】:

  • 这解决了问题。谢谢你。 !^[A-Z]+\ 和 /template/.*\ 之间是否需要有空格?
  • 是的,THE_RQUEST 示例:GET /index.html HTTP/1.1 [A-Z]+ 匹配 GET
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-07
  • 1970-01-01
  • 2018-05-28
  • 1970-01-01
  • 2014-02-26
  • 1970-01-01
相关资源
最近更新 更多