一个网址有两个不同的路径(网址重写)

时间:2016-10-08 09:36:13

标签: php .htaccess mod-rewrite

我有一个网址如下

http://example.com/post/post-title/1

所以它是/ file-name / post-name / page-number

所以我希望第一页显示没有页码。

http://example.com/post/post-title

然后从第二页

http://example.com/post/post-title/2

我的htaccess代码是。

RewriteRule post/(.*)/([0-9]+)$ post.php?id=$1&page=$2 [NC,L]

有没有办法做到这一点?申请你的帮助。

1 个答案:

答案 0 :(得分:2)

您可以有2条规则:

Options -MultiViews
RewriteEngine On

# default rule for page #1
RewriteRule post/([\w-]+)/?$ post.php?id=$1&page=1 [NC,L,QSA]

# to handle URLs with specific page #
RewriteRule post/([\w-]+)/(\d+)/?$ post.php?id=$1&page=$2 [NC,L,QSA]