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 /
This will redirect all traffic from your site with this rule to the site in the command, in this case

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 /, you would not include /home/sites/ in the local path. The first / represents the 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

Forcing WWW in the URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.*)$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]
Was this article helpful?
Thank you!