php - 一个 url 有两个不同的路径(url 重写)

标签 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]

有什么办法吗?应用您的帮助。

最佳答案

你可以为此设置 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]

关于php - 一个 url 有两个不同的路径(url 重写),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39930861/

相关文章:

php - mysql php COUNT 按列 GROUP BY 中的相同值进行回显

javascript - 请求头字段 Cache-Control 不允许

regex - htaccess : remove URL parameter from query string with missing value?

.htaccess - 重写规则以重定向除一条路由之外的整个网站

php - 无法从 JavaScript 获取文本字段值

javascript - 我如何以函数的形式编写这个脚本?

php - Codeigniter 表单验证验证后显示双重重复表单

正则表达式匹配替换帮助

apache - WWW 到 NON WWW Urls(删除 WWW)使用 Apache (.htaccess)

php - .htaccess 结合 2 RewriteRule