AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 877100
Accepted
Putnik
Putnik
Asked: 2017-10-06 09:36:06 +0800 CST2017-10-06 09:36:06 +0800 CST 2017-10-06 09:36:06 +0800 CST

htaccess 替换完整的 URL 而不是 REQUEST_URI

  • 772

我正在做一些应该非常简单但第二天就碰壁的事情。URL 重定向要求是:

  • 非 www => www
  • 非 https => https
  • /file.html=>/utilities/template_handler.php?filename=file.html

问题:当我请求https://example.com/file.html我得到 r=301 到

https://example.com/utilities/template_handler.php?filename=https://www.example.com/file.html

我的.htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteRule ^(.*)\.html$ /utilities/template_handler.php?filename=$1.html [NC]

我正在使用 litespeed 网络服务器,但出于故障排除的目的,我也设置了 Apache,结果相同。打开调试后,我看到:

strip base: '/' from URI: '/file.html'
Rule: Match 'file.html' with pattern '.*', result: 1
Cond: Match 'on' with pattern 'off', result: -1
Rule: Match 'file.html' with pattern '.*', result: 1
Cond: Match 'domain.com' with pattern '^www\.', result: -1
Source URI: 'file.html' => Result URI: 'https://www.example.com/file.html'
Rule: Match 'https://www.example.com/file.html' with pattern '^(.*)\.html$', result: 2
Source URI: 'https://www.example.com/file.html' => Result URI: '/utilities/template_handler.php?filename=https://www.example.com/file.html'
replace current query string with 'filename=https://www.example.com/file.html'

如果我注释掉最后一条规则,我将正确处理前两个要求。

如果请求发送到非 www 或 www 但非 ssl,则会出现相同的错误。

.htaccess
  • 1 1 个回答
  • 5249 Views

1 个回答

  • Voted
  1. Best Answer
    MrWhite
    2017-10-06T11:06:34+08:002017-10-06T11:06:34+08:00
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
    

    您需要在这两个指令中包含L( ) 标志,否则,处理将继续通过文件,并且 URL 将被后面的指令进一步重写(使用前一个指令的输出)。并且由于前面的指令已经触发了外部重定向,因此您将在外部重定向到,而不是内部重写。lastRewriteRuleRewriteRule/utilities/template_handler.php?filename=....

    在请求表单的 URL 时,还应反转这些指令以避免不必要的双重重定向http://example.com/...。(特别是添加了L标志。)

    例如:

    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTPS} off
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    另请注意,我已从否定条件中删除了NC标志。!^www\.由于这是一个否定的正则表达式,因此您希望它在未启动时重定向www- 全部小写。您仍然希望重定向表单的“错误”请求WwW- 但如果您在NC此处包含标志,它们不会。

    如果你愿意,你可以L在最后加入旗帜RewriteRule——尽管这样做是个好习惯。它实际上是隐含的,因为无论如何它是最后一条规则,但是如果您添加了更多指令,那么您可能需要记住添加它。

    • 2

相关问题

  • apache重写以将文件夹分配给域

  • Apache conf 或 .htaccess 规则将 404 重定向到特定文件类型的另一个位置

  • 如何让 Apache2 重定向到子目录

  • .htaccess - 删除所有 cookie

  • 我的 .htaccess 文件是什么意思?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve