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

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

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文件中的代码:

RewriteEngine On

#RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/index\.html$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^/some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
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.

RewriteEngine On

#RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^/index\.html$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^/some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
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规则文件放在一起。

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^index\.html$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^/?some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
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.
RewriteEngine On

RewriteCond %{REQUEST_URI} ^/$ [OR]
RewriteCond %{REQUEST_URI} ^index\.html$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^/?some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^some-path\/([^/.]+)$ $1.html [NC,L]

答案2

得分: 0

这是我的答案
RewriteEngine On

RewriteCond %{THE_REQUEST} ^(?!.*(some-path|robots\.txt)).*$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^some-path\/([^/.]+)$ /$1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^some-path\/([^/.]+)$ /$1.html [L]
英文:

This is my answer
RewriteEngine On

RewriteCond %{THE_REQUEST} ^(?!.*(some-path|robots\.txt)).*$
RewriteRule ^ - [L,R=404]

RewriteCond %{REQUEST_URI} ^some-path/?$
RewriteRule ^ - [L,R=403]

RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^some-path\/([^/.]+)$ /$1.php [L]

RewriteCond %{DOCUMENT_ROOT}/$1.html -f
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:

确定