提问人:Kirill Zaytsev 提问时间:7/31/2023 最后编辑:RavinderSingh13Kirill Zaytsev 更新时间:8/2/2023 访问量:37
在许多索引文件的写入规则时,在文件 .htaccess 中不起作用 RewriteCond
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 代码。我的代码在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$
答:
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]
评论
index.html
index.php
RewriteRule ^some-path\/(([^.]+\.(html|php))|(assets\/.*))$ /$1 [L]
- 这将重写为 .因此,这不符合您所说的要求,“我希望服务器从”some-path“目录中给我一个索引文件。/some-path/index.php
/index.php