在htaccess中为多个页面重写URL

时间:2015-02-11 03:45:54

标签: php apache .htaccess

我需要将两个不同页面的网址重定向到可读的网址。 我的.htaccess看起来像这样

Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ blog-detail.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ blog-detail.php?url=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ page.php?pid=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ page.php?pid=$1 

第一个使用blog-detail.php的网址工作正常。我的URL可以像mysite.com/latest-news-story

但是,除非我删除了博客详细信息重写规则,否则第二个使用page.php不起作用。我怎么能同时拥有这两个?

1 个答案:

答案 0 :(得分:1)

您实际上可以将检查终止/的规则合并为一个。

所以,你实际上只需要以下两条规则。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/-]+)/?$ page.php?pid=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ blog-detail.php?url=$1 [QSA,L]