mod重写帮助将旧网址重定向到新网址

时间:2013-01-14 18:45:06

标签: apache .htaccess url-rewriting permalinks

基本上我试图从这个旧网址移动所有网址:

http://www.gamingonlinux.com/index.php?threads/crusader-kings-2-will-be-released-for-linux-today.1629/

到新网址(当我将网站移动到新系统时,所有外部链接都无效)

http://www.gamingonlinux.com/articles/crusader-kings-2-will-be-released-for-linux-today.1629

有人可以帮我这么做吗?

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI}  ^/index\.php$  [NC]
RewriteCond %{QUERY_STRING} ^[^/]+/([^/]+)/?  [NC]
RewriteRule .*   articles/%1?        [R=301,L]

永久重定向

带有或不带斜线的

http://www.gamingonlinux.com/index.php?anything/query

要:

http://www.gamingonlinux.com/articles/query

答案 1 :(得分:0)

这些行中的某些内容应该可以在主目录中的.htaccess中使用:

RewriteEngine on
RewriteRule ^articles/([^/\.]+)$ index.php?threads/$1/ [L]

如果没有,那么在Google和Bing上有大量关于mod_rewrite和正则表达式的文档!

编辑:我的理解是你希望'/ articles /'url指向'index.php?thread'URL。如果我理解正确,你希望它反过来,就像这样:

RewriteEngine on
RewriteRule ^index.php?threads/([^/\.]+)/$ articles/$1 [L]

EDIT2 :由于网址中的问号,存在一些问题。之后的所有内容都将成为QUERY_STRING的一部分,而不是网址。我已经测试了以下代码,它可以工作:

RewriteEngine on
RewriteCond %{QUERY_STRING} threads/(.*)
RewriteRule ^index\.php$ articles/%1 [L]

希望有所帮助。

相关问题