在为多个索引文件编写规则时,.htaccess文件中的RewriteCond不起作用。

huangapple go评论121阅读模式
英文:

Not work RewriteCond in file .htaccess when write rule for many index file

问题

当我发送请求"http://domain/index.php"或"http://domain"时,我希望服务器返回404状态码。当我发送"http://domain/some-path/index.php"时,我希望服务器从"some-path"目录中返回一个索引文件。但是服务器在所有情况下都返回404状态码。以下是我的htaccess文件中的代码:

  1. RewriteEngine On
  2. #RewriteCond %{REQUEST_URI} ^/$
  3. RewriteCond %{REQUEST_URI} ^/$ [OR]
  4. RewriteCond %{REQUEST_URI} ^/index\.html$
  5. RewriteRule ^ - [L,R=404]
  6. RewriteCond %{REQUEST_URI} ^/some-path/?$
  7. RewriteRule ^ - [L,R=403]
  8. RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
  9. RewriteCond %{REQUEST_FILENAME} !-f
  10. RewriteCond %{REQUEST_FILENAME} !-d
  11. RewriteRule ^some-path\/([^/.]+)$ $1.html [L]

看起来RewriteCond %{REQUEST_URI} ^/index\.html$这一行没有正常工作。

英文:

When i make a request "http://domain/index.php" or such "http://domain" i want the server to give me 404 code. When i make "http://domain/some-path/index.php" i want the server to give me an index file from the "some-path" directory. But server give me 404 code in all cases. There my code in htaccsess file.

  1. RewriteEngine On
  2. #RewriteCond %{REQUEST_URI} ^/$
  3. RewriteCond %{REQUEST_URI} ^/$ [OR]
  4. RewriteCond %{REQUEST_URI} ^/index\.html$
  5. RewriteRule ^ - [L,R=404]
  6. RewriteCond %{REQUEST_URI} ^/some-path/?$
  7. RewriteRule ^ - [L,R=403]
  8. RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
  9. RewriteCond %{REQUEST_FILENAME} !-f
  10. RewriteCond %{REQUEST_FILENAME} !-d
  11. RewriteRule ^some-path\/([^/.]+)$ $1.html [L]

It seems that the line RewriteCond %{REQUEST_URI} ^/index\.html$ does not work correctly.

答案1

得分: 2

请确保在测试URL之前清除浏览器缓存,并确保将.html文件与.htaccess规则文件放在一起。

  1. RewriteEngine On
  2. RewriteCond %{REQUEST_URI} ^/$ [OR]
  3. RewriteCond %{REQUEST_URI} ^index\.html$
  4. RewriteRule ^ - [L,R=404]
  5. RewriteCond %{REQUEST_URI} ^/?some-path/?$
  6. RewriteRule ^ - [L,R=403]
  7. RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [NC,L]
  8. RewriteCond %{REQUEST_FILENAME} !-f
  9. RewriteCond %{REQUEST_FILENAME} !-d
  10. RewriteRule ^some-path\/([^/.]+)$ $1.html [NC,L]

希望对你有帮助!

英文:

With your shown samples and attempts please try following .htaccess rules file.

  • Please make sure to clear your browser cache before testing your URLs.
  • Also make sure to keep your .html files along side with your .htaccess rules file.
  1. RewriteEngine On
  2. RewriteCond %{REQUEST_URI} ^/$ [OR]
  3. RewriteCond %{REQUEST_URI} ^index\.html$
  4. RewriteRule ^ - [L,R=404]
  5. RewriteCond %{REQUEST_URI} ^/?some-path/?$
  6. RewriteRule ^ - [L,R=403]
  7. RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [NC,L]
  8. RewriteCond %{REQUEST_FILENAME} !-f
  9. RewriteCond %{REQUEST_FILENAME} !-d
  10. RewriteRule ^some-path\/([^/.]+)$ $1.html [NC,L]

答案2

得分: 0

这是我的答案
RewriteEngine On

  1. RewriteCond %{THE_REQUEST} ^(?!.*(some-path|robots\.txt)).*$
  2. RewriteRule ^ - [L,R=404]
  3. RewriteCond %{REQUEST_URI} ^some-path/?$
  4. RewriteRule ^ - [L,R=403]
  5. RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
  6. RewriteCond %{DOCUMENT_ROOT}/$1.php -f
  7. RewriteRule ^some-path\/([^/.]+)$ /$1.php [L]
  8. RewriteCond %{DOCUMENT_ROOT}/$1.html -f
  9. RewriteRule ^some-path\/([^/.]+)$ /$1.html [L]
英文:

This is my answer
RewriteEngine On

  1. RewriteCond %{THE_REQUEST} ^(?!.*(some-path|robots\.txt)).*$
  2. RewriteRule ^ - [L,R=404]
  3. RewriteCond %{REQUEST_URI} ^some-path/?$
  4. RewriteRule ^ - [L,R=403]
  5. RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
  6. RewriteCond %{DOCUMENT_ROOT}/$1.php -f
  7. RewriteRule ^some-path\/([^/.]+)$ /$1.php [L]
  8. RewriteCond %{DOCUMENT_ROOT}/$1.html -f
  9. RewriteRule ^some-path\/([^/.]+)$ /$1.html [L]

huangapple
  • 本文由 发表于 2023年7月31日 18:21:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定