清除静态和动态页面 url

Clean url for static and dynamic pages

我的网站混合了没有变量的静态页面(例如:about.php)和一些动态页面(例如:searchresults.php?a=1&b=2)

现在我的 .htaccess 允许显示静态页面,但不允许显示动态页面。然而,我为使动态页面正常工作所做的一切都破坏了静态页面的干净 URL。

RewriteEngine On

DirectoryIndex index.php index.html

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ .php [NC,L]
RewriteRule ^([^\.]+)$ .html [NC,L]

我希望最终结果如下所示:

http://example.com/about.php -> http://example.com/about

http://example.com/searchresults.php?a=1&b=2 -> http://example.com/searchresults/1/2

我想做的事情可行吗?

是的,有可能。

首先,您需要更改规则以删除扩展名。目前,您有两条匹配任何内容的规则(意味着第二条永远不会触发),而第二条规则没有条件。

其次,需要明确指定您的搜索规则。

您的 .htaccess 文件现在应该如下所示:

RewriteEngine on

# Rewrite the search page

RewriteRule ^searchresults/([^/]+)/([^/]+)/?$ searchresults.php?a=&b= [QSA,L]

# Allow requests without php/html extensions

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ .php [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ .html [L]

为您的 htaccess 文件尝试以下代码行。

RewriteEngine on
RewriteRule aboutus/$               about.php   [NC,L,QSA]
RewriteRule searchresults/(.*)/(.*)/$           searchresults.php?a= & b=   [NC,L,QSA]