htaccess Site Redirection Without Parameters

By | September 1, 2008

Sometimes you need to redirect all traffic from one site to another. Very often site structure is different and the need is to send visitors to the main page of the site. This makes Site Redirection tabs of all known panels unusable, as the parameters are being sent to the new site. For example, if you had www.oldsite.com/index.php?f=9 and you’ve set redirection to www.newsite.com/, you will be redirected to www.newsite.com/index.php?f=9.

Sometimes you can omit these parameters (for example, when pointing to a PHP script with at least one parameter), but most probably you won’t need this. How to hide these parameters and send your users to the main page of a new site? Here is an .htaccess solution.

We will need two files: .htaccess in the root directory of your site and a PHP file located anywhere you like. First of all we need to create an .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ http://www.site.com/redirect.php

What does this piece of code do? It redirects all requests to the server (^(.*)$) to a PHP file, that processes redirection. All the unnecessary parameters will be sent to this PHP file and it won’t transmit them further as it contains only one string:

<?
header (“Location: http://newsite.com”);
?>

Can you see the difference? None of old site parameters are sent to the new site, everything looks clear and simple.

I think there is a smarter solution for this, I would encourage you to post it here.

2 thoughts on “htaccess Site Redirection Without Parameters

  1. Tim Green

    Why use the PHP script to handle the redirection? You’ll also lose any page ranking on pages that do this… a simpler alternative would be:-

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ http://www.newsite.com [R=301,L]

    This does the same thing, without the need for a PHP script and preserves your page rank on Google, by telling the search engine that the page has moved…

  2. Tim Green

    Why use the PHP script to handle the redirection? You’ll also lose any page ranking on pages that do this… a simpler alternative would be:-

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ http://www.newsite.com [R=301,L]

    This does the same thing, without the need for a PHP script and preserves your page rank on Google, by telling the search engine that the page has moved…

Comments are closed.