How can I redirect and rewrite my URLs with an .htaccess file?

What do I modify in the following examples ?

You can easily insert the following examples into your .htaccess file as they are shown. If any example contains a hyperlink highlighted in bold, kindly replace it with your actual URL. For example, if the website example.com is mentioned, replace it with your own website’s domain name.

Forcing or removing www in the URL

Forcing www to the URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) https://www.example.com/$1 [R=301,L]

Removing www from the URL

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) https://example.com/$1 [R=301,L]

Redirecting all URLs to a new site

Below two examples redirect all URLs on your current website to a separate website. This feature becomes especially valuable after a website moves to a different domain.

Option 1

Redirect 301 / https://example.com/

Option 2

In this scenario, your previous site, example.com, will redirect while retaining all its URLs to a new site called new.com.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule (.*)$ https://www.new.com/$1 [R=301,L]
</IfModule>

Redirecting a single URL

By employing the Redirect directive in a.htaccess file, you can effortlessly redirect users from an outdated page to a new one, without the need to retain the previous page. For example, if your initial index file is named index.html and you subsequently change its name to main.html, you can create a redirect to guide users from index.html to main.html. For example:.

Redirect to a local site file

Redirect /path_to_old_file/old.html /path_to_new_file/new.html

Redirect to an external site file

Redirect /path_to_old_file/old.html 
https://www.example.com/new/file/new.html

The first path

The first path to the old file should be a local UNIX path, not the complete path. If the .htaccess file is located in the directory /example.com, you would not include /home/username/example.com in the local UNIX path.  The first / represents the example.com directory. If the old file was in that directory, you would follow the / with the old file name.

The second path

The second path to the new file can be a local UNIX path, but can also be a full URL to link to a page on a different server or the same server.

Examples of redirects

Redirecting from a directory to an HTML file

RedirectMatch 301 ^/blog/about /blog/about.html

Redirecting from an index.html file to a different directory

Redirect /index.html /new/

Redirecting from index.html to default.html

Redirect /index.html /default.html

Redirecting a local /private directory to another site’s private directory

Redirect /private/ https://www.example.com/private/

Loading a .gif file from a different site

Ensure that the other website is under your ownership. It is advisable to avoid linking files from external websites.

Redirect /img/logo.gif https://www.example.com/images/logo.gif

Using Regular Expressions

If you wish to redirect something using a Regular Expression, employ the RedirectMatch directive.

RedirectMatch "^/oldfile\.html/?$" "https://example.com/newfile.php"

Redirecting error messages

You can also redirect 404 errors. Instead of displaying a 404 error page, this redirects to the main page of the website.

ErrorDocument 404 https://example.com/

Redirecting an old directory to a new directory

 This redirects files in an old directory (/blog/archives) to a new directory (/archives). The file must exist in the new directory to function.

RewriteRule ^blog/archives/(.*)$ /newarchives/$1 [R=301,NC,L]

Redirecting an old directory to the home directory

This redirects files in an old directory (/blog/archives) to the home directory.

If you add a specific file (example.com/blog/archives/new.html) to the end, it must exist in the home directory to load.

If only the subdirectory is visited, the home directory loads. If you add an index file (such as index.html), it will load.

RewriteRule ^blog/archives/(.*)$ /$1 [R=301,NC,L]

Redirecting non-existent pages to the index.php page.

If someone tries to access a page that doesn’t exist, they are shown a 404 error. Instead of redirecting any request to a non-existent page, you can redirect it to your index.php file (or any index file) by adding the following code in your 2.htaccess:

Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

If your homepage is not index.php, simply modify the last line to point to your actual index file. After the visitor is guided back to your homepage.

Loading a Subdirectory Automatically

This example redirects the ROOT domain’s URL to any subdirectory. In this instance, it automatically loads example.com/subdir1:

RewriteEngine on
RewriteRule ^$ /subdir1/ [L]

Rewriting a URL

This example changes the URL to a different website. This rewrites example.com/1.html to example.com/abc.php?id=1.

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+).html /abc.php?id=$1 [QSA,L]

The following explains the rules above:

([0-9]+) : allows any digit, and only any digit, 1 or more times.
([a-z-]*) : allows any lowercase letter, plus “-” for word separation, 0 or more times. If you want it to support uppercase too, use “([a-zA-Z-]*).

For example: RewriteRule ^place/([a-zA-Z-]*).html /place/abc.php?id=$1 [QSA,L]

[QSA,L] :  appends this to your internal scripting query string, and makes it the Last rewrite rule executed.

Once you have applied this technique, you will be able to access the webpage using either the address type. This is useful for modifying a website that was not originally created with mod_rewrite in mind. This is advantageous as it preserves any bookmarks that users have saved on their computers.

Rewriting non-existing links to index.php

The following redirects all links to non-existent files or folders to index.php. Nevertheless, if the file or directory is already present, it functions as expected:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>