Search code examples
.htaccessurl-rewritinghttp-redirecthttp-status-code-301

HTACCES rewrite rules without parameters


I want to make 301 redirection from my old url : www.test.com/?page=mypage&lang=mylanguage

to the new www.test.com/mylanguage/mypage

I was using this:

RewriteCond %{QUERY_STRING} ^page=mypage&user_lang=lang$ [NC]
RewriteRule ^(.*)$ http://test.joskin.com/lang/mypage [R=301,L,NE]

but when I click on my old link, it redirect me to http://test.joskin.com/lang/mypage?page=mypage&user_lang=lang

how can I take off these parameters from my new url ?

Best regards.


Solution

  • You were really close! Use this instead:

    RewriteCond %{QUERY_STRING} page=mypage&lang=mylanguage$ [NC]
    RewriteRule ^(.*)$ http://test.joskin.com/lang/mypage? [R=301,L,NE]
    

    The change that I made here is to add an ? on to the end of your rewritten URL. The ? at the end of the redirected URL is there so that the query string does not appear on the end of the URL again.

    I removed ^ from the query string, it is not needed.

    I also updated the query to match the URL that you displayed at the top of your question. I will leave that to you if you wish to change it back.

    Make sure you clear your cache before testing this.