如何将文件夹/page.html重定向到htaccess中的单独页面



我有以下。htaccess文件工作正常,现在我试图添加另一个文件夹,但这是不工作。我该怎么做?

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*).html$ page.php [QSA,L]

正如您在上面的htaccess中看到的,它只是匹配任何*.html并发送到page.php

现在我想为somefolder/somepage.html添加另一行,它应该重定向到post.php,但问题是第一个没有任何文件夹,第二个有文件夹,但都有。html,所以第一个条件触发。

提前感谢。

所以你需要改变规则的顺序,或者添加一个条件,或者使用更具体的规则,对吗?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?([^/]+)/.+.html$ $1/post.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?[^/]+.html$ page.php [QSA,L]

或者,如果您需要针对特定文件夹的更具体,更不通用的方法:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?somefolder/.+.html$ somefolder/post.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/?[^/]+.html$ page.php [QSA,L]

最新更新