Search code examples
.htaccesshttp-redirectmod-rewriteurl-rewriting

How to prevent append parameter in 301 redirect


I am using this redirect command in .htaacess file.

Redirect 301 /old-path.html /new-path.html

It shows me /new-path.html?page=old-path

Probability, appended parameter is result of some RewriteRule and RewiteCond in my htaccess.

I don't like to edit my original htaccess codes.

I found below command is very close to the result.

RedirectMatch 301 /old-path.html /new-path.html?

The result is /new-path.html? which has only an extra ?


Solution

  • Don't use RedirectMatch because it doesn't have a way to discard old/previous query string.

    It is better to use a RewriteRule like this:

    RewriteRule ^old-path\.html$ /new-path.html? [L,R=301,NC]
    

    Trailing ? in target will discard any query string.

    On Apache 2.4+ you can also use:

    RewriteRule ^old-path\.html$ /new-path.html [L,R=301,NC,QSD]
    

    As a thumb rule place all R=... rules on top of your .htaccess before other rules. This will make these rules work before other internal rewrite rules that may modify request URI or query string.