In this guide we will go through some of the .htaccess setups that can be used for several types of redirects, including to another site entirely or to www.

Redirecting all traffic

Redirect 301 / https://example-site.co.uk/
This will redirect all traffic from your site with this rule to the site in the command, in this case https://example-site.co.uk/

Redirecting of a single URL

Using Redirect in an .htaccess file allows the redirection of users to a new page without having to keep the old one. e.g, if you use index.html as your index file and then later rename index.html to home.html, you could set up a redirect to send users from index.html to home.html. For example:

Redirecting to a file:
Redirect /oldpath/old-file.html /newpath/new.html

The first path to the old file must be to a local path, NOT the full path. If the .htaccess file is in the directory /test.com, you would not include /home/sites/test.com in the local path. The first / represents the test.com directory. If the old file was in that directory, you would follow the / with the old file.

The path to the second file can be a local path like above, but you can also have this as a full path drawing from an external server like so:

Redirect /oldpath/old-file.html http://www.newtestsite.com/new.html

Forcing WWW in the URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example-site.co.uk
RewriteRule (.*) http://example-site.co.uk/$1 [R=301,L]


Redirecting requests to non-existing links to index.php

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Was this article helpful?
Cancel
Thank you!