在htaccess

时间:2017-08-21 17:06:07

标签: .htaccess redirect mod-rewrite url-redirection

我想重定向以下网页:

/category-name/post-name.html?id=1234

要:

/category-name/1234-post-name.html

如何使用htaccess

执行此操作

我尝试过:

RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]

但这是一个持续的重定向。

2 个答案:

答案 0 :(得分:1)

试试这个

确保网址是根网址

实施例: -

www.foo.com/category-name/post-name.html

仅当项目网址与上述网址相同时才会生效。

www.foo.com/blog/category-name/post-name.html

这不起作用,你需要相应地更新RewriteBase网址。

这是条件

RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d

这里是RewriteRule

RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]

这应该有用..

答案 1 :(得分:1)

您可以在站点根目录中使用这些规则.htaccess:

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]