htaccess重定向有和没有参数

时间:2014-11-18 12:48:12

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

我正在尝试执行以下重定向:

  • 如果用户来自:www.example.com/folder/sub-folder/,则会重定向到www.example.com/filehtml.html

    这是正常的。

  • 如果用户来自:www.example.com/folder/sub-folder/index.html,则会将其重定向到www.example.com/filehtml.html

    这不起作用。

  • 如果用户来自:www.example.com/folder/sub-folder/?id=1&id2=2,则会将其重定向到www.example.com/filehtml.html?id=1&id2=2

    这是正常的。

  • 如果用户来自:www.example.com/folder/sub-folder/index.html?id=1&id2=2,则会将其重定向到www.example.com/filehtml.html?id=1&id2=2

    这不起作用。


Redirect 301 /folder/sub-folder/ /filehtml.html
RewriteCond %{HTTP_HOST}  ^www\.site\.pt$ [NC]
RewriteCond %{QUERY_STRING}  ^$
RewriteRule ^folder/sub-folder/index\.html$ http://www.example.com/filehtml.html%{REQUEST_URI} [R=301,NC]

我做错了什么?

1 个答案:

答案 0 :(得分:0)

使用此规则替换所有代码:

RewriteEngine On

RewriteRule ^folder/sub-folder/ /filehtml.html [L,NC,R=302]

QUERY_STRING会自动转移到/filehtml.html

  • 确保将其放在root .htaccess
  • 确保这是第一条规则