将4个不同的网址重定向到一个网址

时间:2015-12-14 17:48:01

标签: .htaccess mod-rewrite url-rewriting rewrite

我有一个查询,我希望网址重写向导可以帮助我,因为这个有点超出我的范围。

我有以下网址:

http://www.domain.com/region/news/123456-article-name-goes-here.html http://www.domain.com/region/news/123456-article-name-goes-here.html?get_var=1234 http://www.domain.com/mobile/region/news/123456-article-name-goes-here.html http://www.domain.com/mobile/region/news/123456-article-name-goes-here.html?get_var=1234

我需要按以下格式将所有这些变体重定向到网址:

http://www.domain.com/region/article-name-goes-here/

所以我需要删除'移动/',删除'新闻/',删除' 123456 - ',删除.html,删除任何GET变量,然后添加一个尾部斜杠。

最好的方法是做什么?

非常感谢!

2 个答案:

答案 0 :(得分:1)

I'd say this is what you need:

RewriteEngine on
RewriteRule ^/region/news/\d+-(.+)\.html$ http://www.domain.com/region/$1/ [R=301,L]
RewriteRule ^/mobile/region/news/\d+-(.+)\.html$  http://www.domain.com/region/$1/ [R=301,L]

You could do that with one rule, but the above is easier to understand, so I'd use two rules.


Note: this is the version for the host configuration. If you need to use .htaccess style files instead, then you have to remove the leading slashes from the patterns in the rules, since those files work on relative paths. Note however that whenever possible you should prefer the host configuration variant over htaccess style files. Those files are notoriously error prone, cumbersome to implement, harder to debug and really slow the server down. So only rely on them if you really have to, for example if you do not have access to the host configuration.

答案 1 :(得分:0)

您只能使用一个规则来重定向这些网址:

尝试:

 RewriteEngine on
 RewriteRule ^(?:mobile/)?/([^/]+)/([^/]+)/([^-]+)-([^.]+)\.html$ http://example.com/$1/$4/? [NC,L,R]

?在规则结束时很重要,因为它会丢弃来自url的原始查询字符串