Search code examples
php.htaccess

htaccess redirects for subdomain and all links


Does anyone know of any 301 .htaccess rules than would cover all links on a website. Here's a few examples:

Examples:

https://support.olddomain.com/
https://support.newdomain.com/

https://support.olddomain.com/id.php?pp=sdf-sdf-sdf&r=13
https://support.newdomain.com/id.php?pp=sdf-sdf-sdf&r=13

Solution

  • If the two hostnames (ie. support.olddomain.com and support.newdomain.com) point to different servers then you can use a simple mod_alias Redirect in the .htaccess file at the old-domain. For example:

    # Redirect everything to the new-domain (like for like URLs)
    Redirect 301 / https://support.newdomain.com/
    

    However, if the two hostnames point to the same place then you need to use mod_rewrite and explicitly check the requested hostname in order to avoid a redirect loop. For example:

    RewriteEngine On
    
    # Redirect everything to the new-domain (like for like URLs)
    RewriteCond %{HTTP_HOST} =support.olddomain.com
    RewriteRule ^ https://support.newdomain.com%{REQUEST_URI} [R=301,L]
    

    Both sets of rules do the same thing... ie. 301 redirect https://support.newdomain.com/<url-path>?<query-string> to the same <url-path> and <query-string> at https://support.newdomain.com/.