【问题标题】:Why does mod_rewrite sometimes appear to do an external redirect when the trailing slash is missing?为什么当尾部斜杠丢失时,mod_rewrite 有时会出现外部重定向?
【发布时间】:2015-12-15 05:46:34
【问题描述】:

我正在使用以下重写规则:

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f [or]
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html -f [or]
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}index.html -f
RewriteRule ^(.+) static/$1

它们都成功地在 static 目录中提供文件,而无需在 url 中包含 static/。唯一的区别是,当我没有尾部斜杠时,static 目录会像外部重定向一样添加到 URL。

因此,

http://example.com/products/index.html --> http://example.com/products/index.html

http://example.com/products/ --> http://example.com/products/

但是,

http://example.com/products --> http://example.com/static/products/

有没有办法让索引文件从/static/products 目录提供,而不像其他两种情况一样在url 中显示static 目录?

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    我敢打赌,有一种更简单的方法可以解决这个问题,但我终于想出了一个可行的组合:

    RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}index.html !-f  
    RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html -f  
    RewriteRule ^(.+) static/$1/
    
    RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}index.html -f [or] 
    RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f 
    RewriteRule ^(.+) static/$1
    

    【讨论】:

      猜你喜欢
      • 2016-08-13
      • 2015-06-10
      • 2016-10-29
      • 1970-01-01
      • 2012-09-25
      • 2022-12-25
      • 2018-07-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多