url在php中重写更改帖子网址

时间:2015-11-06 17:26:52

标签: apache .htaccess mod-rewrite friendly-url

我想要更改网址:

http://example.com/posts.php?action=view&id="a number"

http://example.com/posts/"a number"

我将这些内容写入.htaccess文件,但它们无法正常工作:

RewriteEngine On
RewriteRule    ^posts/([0-9]+)/?$    posts.php?action=view&id=$1    [NC,L]
RewriteRule    ^posts/?$    posts.php    [NC,L]

2 个答案:

答案 0 :(得分:0)

您可以在.htaccess文件中尝试以下

.htaccess文件代码:

RewriteEngine On
RewriteRule   ^posts/([0-9]+)   /posts.php?action=view&id=$1    [QSA,L]

QSA 表示如果查询字符串与原始网址一起传递,则会将其附加到重写中。

L 表示如果规则匹配,则不再处理此规则下方的重写规则

答案 1 :(得分:0)

就像这样:

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\?action=view&id=(\d+) [NC]
RewriteRule ^posts\.php$ /%1/%2? [R=301,L,NC]

RewriteCond %{THE_REQUEST} ^GET\ /(posts)\.php\ HTTP [NC]
RewriteRule ^posts\.php$ /%1/? [R=301,L,NC]

RewriteRule ^(posts)/(\d+)/?$ /$1.php?action=view&id=$2 [L]
RewriteRule ^(posts)/?$ /$1.php [L]