301重定向:将带.php的网址重定向到文件夹

时间:2017-11-09 10:54:39

标签: .htaccess redirect

在.htaccess中我想重定向这样的网址:

/products.php/a7-frames
/products.php/a6-frames

到此:

/picture-frames/a7-frames
/picture-frames/a6-frames

因此需要用相框替换products.php。 经过很多谷歌搜索后我尝试了这个:

RewriteBase /
RedirectMatch 301 (.*)\.php/?$ https://www.domainname.com/picture-frames$1

但它不起作用,如果我输入此URL:/products.php/a7-frames浏览器说有太多的重定向并转到:

/picture-frames/index

它将products.php替换为画框,这很棒,但我不确定为什么它会在末尾添加“index”而不是url的/ a7-frame部分?我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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

RewriteEngine On

# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+products\.php/([^\s?]+) [NC]
RewriteRule ^ /picture-frames/%1? [R=302,L,NE]

# internal forward from pretty URL to actual one
RewriteRule ^picture-frames/([^/]+)/?$ products.php/$1 [L,NC]

确保在测试此更改之前清除浏览器缓存。

相关问题