Apache : 301 Redirect: Without creating file

Discussion in 'Apache' started by messenger, Jul 16, 2013.

  1. #1
    Hi,
    We are redoing our website and we have removed / renamed a few old pages.
    I was wondering if I can do 301 redirect for removed / renamed pages without having to actually create them?
    To explain with an example:
    On old website : www.domain.com/mypage.php
    On new website: www.domain.com/mynewpage.php
    I created a 301 redirect in .htaccess redirecting mypage.php to www.domain.com/mynewpage.php without physically creating "mypage.php" file.
    When I access "www.domain.com/mypage.php", I get a 404 page not found error.
    When I create a blank "mypage.php", the redirection works fine.

    Is it possible to do a 301-redirect without having to create old files ?

    Thank you for the support.

    Best regards,
    Sans
     
    messenger, Jul 16, 2013 IP
  2. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #2
    yes, it is. you can use something like this in your .htaccess:
    RewriteEngine on
    RewriteRule ^mypage.php$ http://www.domain.com/mynewpage.php? [R=301,L]
    Code (markup):
     
    RoseHosting, Jul 17, 2013 IP
  3. messenger

    messenger Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #3
    Thx @RoseHosting
    yes it worked. Not sure why it did not work when I tried something similar previously.
    Do I need the trailing question mark after "mynewpage.php" ??
    Thou redirection worked with and without the '?', but wanted to understand the syntax.

    Thx again
     
    messenger, Jul 17, 2013 IP
  4. RoseHosting

    RoseHosting Well-Known Member

    Messages:
    230
    Likes Received:
    11
    Best Answers:
    11
    Trophy Points:
    138
    #4
    hi,

    the question mark is there to prevent a query string to be transferred to the new url.

    for example, if someone access www.domain.com/mypage.php?name=foo it will be redirected to mynewpage.php without the query string of '?name=foo'.

    I assumed you did not use query strings in your old url. If you are, then remove the question mark so the query strings are passed over to the new url

    you are welcome
     
    RoseHosting, Jul 17, 2013 IP
  5. messenger

    messenger Member

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    36
    #5
    Thx again for the explanation!
    Appreciate it
     
    messenger, Jul 17, 2013 IP