.htaccess URL如果参数不存在,则重写

时间:2015-09-07 08:49:34

标签: php apache .htaccess mod-rewrite url-rewriting

以下是我在.htaccess文件中使用url重写的代码

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule pages/(.*)/? /page/index.php?name=$1 [NC,L]
如果网址为www.example.com/pages/2,则从上面的代码

然后实际上转到网址www.example.com/page/index.php?name=2

但我也希望这样:如果页面请求是www.example.com/pages/2/user/rahul,那么它也应该转到相同的网址www.example.com/page/index.php?name=2&user=rahul

所以做上面的事情我做了这样的事情:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule pages/(.*)/? /page/index.php?name=$1 [NC,L]
RewriteRule pages/(.*)/user/(.*)/? /page/index.php?name=$1&user=$2 [NC,L]

但它确实按我的意愿工作。

1 个答案:

答案 0 :(得分:0)

尝试:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteRule ^pages/([^/]+)/user/([^/]+)/?$ /page/index.php?name=$1&user=$2 [NC,L]

RewriteRule ^pages/([^/]+)/?$ /page/index.php?name=$1 [NC,L]