#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0

    .htaccess redirect retaining query string?


    Is it possible to set up a series of redirects in a .htaccess file so that "mydomain.com/forthcoming.php?id=101" can be redirected to "mydomain.com/archive.php?id=101" without using a rewrite rule?

    I only want certain id's to be redirected, while others stay as they are.

    Using the standard redirect doesn't work:
    Code:
    Redirect /forthcoming.php?id=101 http://www.mydomain.com/archive.php?id=108
    Would I be better looking to achieve this with PHP, given it'll be the most recent id's that stay while the older id's get redirected. Something along the lines of, if id is less than x redirect to... otherwise go ahead and render the page?
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    Since you want to use the exact same query string,
    Code:
    Redirect /forthcoming.php /archive.php
    might actually work. Not sure because there's no documentation about what happens to query strings, but I'll assume that it's preserved.

    It does have the side effect of allowing anything starting with "/forthcoming.php/": it would redirect /forthcoming.php/asd/123 to /archive.php/asd/123. If that's a problem then mod_rewrite is the next best option, followed by a PHP redirect.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    6
    Rep Power
    0
    Originally Posted by requinix
    Since you want to use the exact same query string,
    Code:
    Redirect /forthcoming.php /archive.php
    might actually work. Not sure because there's no documentation about what happens to query strings, but I'll assume that it's preserved.

    It does have the side effect of allowing anything starting with "/forthcoming.php/": it would redirect /forthcoming.php/asd/123 to /archive.php/asd/123. If that's a problem then mod_rewrite is the next best option, followed by a PHP redirect.
    The problem is that I don't want everything to redirect. It's a gallery website where exhibitions move from forthcoming to current and finally to archive and the redirect needs to push old forthcoming links to the archive - the database entry id being a constant.

    What I have since managed to work out is setting up a check in PHP to see if the exhibition is marked as 'archived' in the database and if so redirect:
    Code:
    if ($archive == "yes") {
    Header( "HTTP/1.1 301 Moved Permanently" ); 
    Header( "Location: http://www.mydomain.com/archive/exhibitions/index.php?id=$id" );
    }
    I should also do something similar to catch the current exhibition too. As far as I can tell this is fairly SEO friendly which is the priority, to get people to the right page from search engine results showing old links and have search engines update their results over time.

    If anyone has a better suggestion feel free

    thanks
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,143
    Rep Power
    9398
    If there's any kind of conditional logic then you need PHP to do it. Like you've done.

IMN logo majestic logo threadwatch logo seochat tools logo