非常简单301 htaccess重定向无法正常工作?

时间:2018-02-02 13:03:51

标签: apache .htaccess redirect mod-rewrite

非常简单的网址重定向:

http://xxxx.net/index.php?k=Nomor-Bangkok&page=0

为:

http://xxxx.net/Nomor-Bangkok/online-0

当我尝试此操作时(旧网址到新网址):

Redirect 301 /index.php?k=Nomor-Bangkok&page=0 /Nomor-Bangkok/online-0

我收到了200个没有重定向的标头响应。

但是当我转过身时(旧网址的新网址):

Redirect 301 /Nomor-Bangkok/online-0 /index.php?k=Nomor-Bangkok&page=0

然后我从新旧URL获得301重定向。但那不是我想要的。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

来自docs:" mod_alias旨在处理简单的URL操作任务。对于更复杂的任务,例如操作查询字符串,请使用mod_rewrite提供的工具。"

因此,您可以这样做:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^k=Nomor-Bangkok&page=0$ [NC]
RewriteRule index.php /Nomor-Bangkok/online-0? [NC,L,R]

或甚至,如果这是您需要的更一般的规则:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^k=([a-zA-Z0-9-]+)&page=([0-9]+)$ [NC]
RewriteRule index.php /%1/online-%2? [NC,L,R]

会将/index.php?k=AnyString&page=AnyNumber重定向到/AnyString/online-AnyNumber