我有这些文件夹:
projects/
├─ foobar.html
projects.html
projects_page=1.html
projects_page=2.html
这个请求的 uri:
http://localhost:8888/subdir/en/projects?page=2
还有这个 htaccess:
DirectoryIndex disabled
Options -MultiViews
Options -Indexes
DirectorySlash Off
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.+)$ [NC]
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteCond %{QUERY_STRING} page=([0-9]+) [NC]
RewriteRule ^/ %1_page=%2.html [L,R=307,QSD]
我预计会输出
projects_page=2.html
,但我得到的是 403 禁止。如果我启用目录列表 (+Indexes
),我会得到目录列表。
当请求存在文件夹的 uri 时,如何在 htaccess 中定义以优先重写文件?
在 MAMP 6 上运行。
结果在本地对我有用的是:
RewriteCond %{REQUEST_URI} !\.html$ [NC]
RewriteCond %{QUERY_STRING} page=([0-9]+) [NC]
RewriteRule ^(.*)$ $1﹖page=%1.html [L]
重要的变化是,当您不想重定向(有时会发生)时,不要使用
[R]
,而是将 RewriteCond %{REQUEST_URI} !\.html$ [NC]
与 [L]
结合使用,或者使用 [END]
代替 [L]
。