Htaccess [SOLVED]: Apache rewrite to new page with parameters included

Htaccess [SOLVED]: Apache rewrite to new page with parameters included

Home Forums htaccess Htaccess [SOLVED]: Apache rewrite to new page with parameters included

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #36973

    Anonymous

    QuestionQuestion

    I have external sites linking to my page at:

    http://www.example.com/old.php?page=55555

    but now I need them redirected to:

    http://www.example.com/new.php?page=55555

    A have seen redirects solved, but those were different, they were translated to a different kind of url (eg. example.com/page/55555) or other domain, not the same domain and same url construction.

    I did get it to work without the parameters using
    RewriteRule old.php new.php, but this does not work when passing the params.

    Just renaming the page is not an option 🙂
    Thanks !

    EDIT:

    This is the code that works, but only without the parameters. Whenever you add “?page=55555”, it does not work anymore.

    RewriteRule old.php new.php [R=302,NC]
    

    This is another variation I had tried, but could not get it to work:

    RewriteRule ^old.php?([0-9]*) new.php?page=$1 [NC,QSA]
    

    EDIT2:

    The answer given works, however it does only when I remove the following 4 lines from the .htaccess file:

    RewriteCond %{QUERY_STRING} !marker  
    RewriteCond %{QUERY_STRING} page=([0-9]+)
    RewriteRule ^/?new.php$ %1? [R=301,L]
    RewriteRule ^/?p([-a-zA-Z0-9_+]+)$ new.php?marker&page=$1 [L]
    

    These 4 lines make it possible to enter
    http://www.example.com/p4444
    in the browser, and it will open
    http://www.example.com/new.php?page=4444
    (but still mask it with http://www.example.com/p4444)

    I would like both old.php and new.php to work this way. I hope this is clear.

    #36974

    Anonymous

    Accepted AnswerAnswer

    The [QSA] flag should be used:

    RewriteEngine On
    RewriteBase /
    RewriteRule old.php new.php [R,NC,QSA,L]
    

    Source: https://stackoverflow.com/questions/48040859/apache-rewrite-to-new-page-with-parameters-included
    Author: Ben
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.