在许多索引文件的写入规则时,在文件 .htaccess 中不起作用 RewriteCond

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

提问人:Kirill Zaytsev 提问时间:7/31/2023 最后编辑:RavinderSingh13Kirill Zaytsev 更新时间:8/2/2023 访问量:37

问:

当我发出“http://domain/index.php”或“http://domain”请求时,我希望服务器给我 404 代码。当我制作“http://domain/some-path/index.php”时,我希望服务器从“some-path”目录中给我一个索引文件。但是服务器在所有情况下都给我 404 代码。我的代码在htaccsess文件中。

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$

Apache .htaccess mod-rewrite url-rewriting

评论

0赞 CBroe 7/31/2023
现在是哪一个,还是?index.htmlindex.php
0赞 CBroe 7/31/2023
RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]- 这将重写为 .因此,这不符合您所说的要求,“我希望服务器从”some-path“目录中给我一个索引文件。/some-path/index.php/index.php
0赞 RavinderSingh13 7/31/2023
通过查看您的个人资料,您知道您从未接受过任何答案。当你看到答案时,你可以让回答者知道它是怎么回事。现在,您也可以转到旧答案并接受适用的答案。它将帮助访问者查看哪个答案对问题海报、欢呼有帮助。

答:

2赞 RavinderSingh13 7/31/2023 #1

使用您显示的示例和尝试,请尝试以下 .htaccess 规则文件。

  • 在测试您的 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]
0赞 Kirill Zaytsev 8/2/2023 #2

这是我的答案 RewriteEngine 开启

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]