Every so often I need to redirect from one domain to another.
Maybe I’m configuring both a .com
and .co.uk
domain to point to just one of them.
Maybe I need to redirect an directory/path to a different domain as part of a site migration or segmentation (e.g. redirecting https://example.com/account
to https://account.example.com
).
Response codes
Before setting up a redirect, make sure you know what kind of redirect you want. There are several different response codes which change how search engines (e.g. Google) behave when the see the redirect.
301 Moved Permanently
302 Found
303 See Other
e.g. for use after logging in, where the new page must be fetched with a GET request.307 Temporary Redirect
if in doubt, this is a modern safe option. Behaves similar to302
but works with both GET, POST and other methods.308 Permanent Redirect
search engines will reindex the page under its new url. Behaves similar to301
but works with both GET, POST and other methods.
301
and 302
used to be the defaults for redirects, but they have been misused over the years so in general you only ever want to use 303
, 307
and 308
as the old codes have undefined behaviour. The newer response codes define what type of requests should be sent to perform the redirect. 303
will only ever redirect with a GET request, so is ideal when loading a page in response to some user action (such as successfully logging into a website). 307
and 308
will re-issue the previous request (whatever method was used) to the new location, so are useful for all kinds of redirects, including full-site redirects.
Redirecting a single page
If you need to redirect a single page, then on a PHP webserver it can ve as simple as creating a file index.php
with the following:
<?php
header('Location: https://example.com/new-page', true, 307);
However, by default this only works if the user navigates to the root domain (e.g.https://example.co.uk
). If you want this to also work on different paths (e.g. https://example.co.uk/old-page
) then you need some webserver configuration. The configuration itself depends on your webserver. For apache, the following works if placed in a .htaccess
file.
Redirecting whole site with a php file
Make a file .htaccess
with the following contents next to your index.php
file (from above).
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
Although a safer version, with comments explaining the various options is here. Choose whichever suits your usage best.
<IfModule mod_rewrite.c>
RewriteEngine On
# Some hosts may require you to use the `RewriteBase` directive.
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the index.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Redirecting whole site with htaccess
To redirect an entire domain to example.com
, including the path and all query parameters.
RewriteEngine on
RewriteRule ^(.*)$ https://example.com/$1 [R=307,L]
Use a 308
redirect for permanent redirects.
Redirecting www to non-www (naked) domains
To redirect www.example.com
to example.com
, including the path and all query parameters.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.*$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=307,L]
Use a 308
redirect for permanent redirects.