Tutorial - Rewrite the URL for accessing your website using mod_rewrite via the .htaccess file

This guide explains how to rewrite the URL for accessing your website using mod_rewrite via the .htaccess file

Last updated 22nd December 2022   

Objective

mod_rewrite is one of the modules available on the HTTP Apache web server. Apache is installed on our entire shared hosting infrastructure. With this web server, you can manage all HTTP requests sent to your Web Hosting plan.

For example, Apache retrieves HTTP requests generated by your website’s visitors’ internet browsers, and returns the content requested by those same requests. Internet browsers then display the content of your website to your visitor.

For example, the mod_rewrite allows you to rewrite and redirect:

  • A visitor who enters your URL in “HTTP” to your website’s URL in “HTTPS”.
  • All of the URLs used for your website to a specific folder or file.
  • A visitor who enters your URL without “www” directly to the URL of your website with “www”.

The mod_rewrite module offers an infinite number of possibilities. Below are some of the most common use cases.

If you would like to learn more about how to use the Apache mod_rewrite module, or if the example you are looking for is not present in this tutorial, please refer to the official Apache documentation.

This tutorial explains how to rewrite the URL for accessing your website using mod_rewrite via the .htaccess file   

Requirements

Instructions

OVHcloud provides services that you are responsible for with regard to their configuration and management. It is therefore your responsibility to ensure that they function properly.

This tutorial is designed to help you with common tasks. Nevertheless, we recommend contacting a specialist provider if you encounter any difficulties. We will not be able to assist you. You can find more information in the Go further section of this guide.

The following examples should be set up in a ".htaccess" file. Please note that the rules you define in this file have a direct impact on your website. Always verify the rules you add before applying them to your website.

The ".htaccess" file in which you are going to configure the Apache mod_rewrite can be placed in several different folders. Be sure to follow the rule of only one ".htaccess" file per folder or subfolder.

The parameters defined in a ".htaccess" file apply to the directory in which it is present, as well as to all its subdirectories.

To edit (or create) directories, log in to your hosting plan’s FTP space. If you need help with this, please refer to our guide on Accessing your Web Hosting plan’s storage space.

Below are some of the most common examples of using Apache mod_rewrite. Some of them can also help the SEO your website.

Redirect all HTTP requests to a single file in your site

Edit the ".htaccess" file in the root directory that contains your website. Place the following code inside (by replacing the test.php in our example with the name of your own file):

RewriteEngine On
RewriteRule .* test.php

In our example, all requests made to your website are redirected to the test.php file.

Redirect some of the HTTP requests to a single file in your website

Edit the ".htaccess" file in the root directory that contains your website. Put the following code inside (replacing in our example the values thetest and /test_wslash/test.php with the names of your own files):

RewriteEngine On
RewriteRule thetest /test_wslash/test.php

In our example, all HTTP requests that contain /thetest are redirected to the /test_wslash/test.php file.

Redirect your domain name to its "www" subdomain

This rewrite rule forces your website’s address/URL to include its subdomain "www".

Edit the ".htaccess" file in the root directory that contains your website. Put the following code inside (replacing in our example domain.tld with your own domain name):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.tld$
RewriteRule ^(.*) http://www.domain.tld/$1 [QSA,L,R=301]

This URL rewrite can help SEO your website.

Redirect requests to a particular folder without displaying the folder

When you use an OVHcloud shared hosting plan, your domain name (for example domain.tld) is declared as a Multisite to display the content of a target folder also called root folder. You can customise the name of this root folder.

Please refer to our guide on Hosting multiple websites on your Web Hosting plan if you would like more information on this subject.

Some users do not place their websites directly in the root folder, but inside a subfolder (for example: MyWebsite) inside this root folder.

In this case, the URL to access the site will be as follows: http://domain.tld/MyWebsite.

If your website is not located directly in the root folder declared for your domain name in Multisites, and you do not want to display the folder name in your website’s URL, edit the ".htaccess" file located in the root directory of your website.

Place the following code inside (replacing the values domain.tld with your domain name, and MyWebsite with the name of your own folder):

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.tld
RewriteCond %{REQUEST_URI} !^/MyWebsite
RewriteRule ^(.*)$ /MyWebsite/

In our example, this forces your website address to be displayed as http://domain.tld, whereas in reality the page called is http://domain.tld/MyWebsite.

Automatically redirect a visitor to use HTTPS when the website was accessed using a HTTP URL

SSL certificates encrypt the data exchanged in HTTP with your website. This prevents people or malicious robots from collecting data exchanged between the site and the visitor, such as bank details.

If you do not have an SSL certificate, please refer to our guide on Managing an SSL certificate on an OVHcloud shared hosting plan.

Some of your visitors may forget to enter the URL to access your site by https://: This poses a significant risk to the data exchanged between your website and their web browsers.

To prevent this, edit the ".htaccess" file located in the root directory of your website. Put the following code inside (replacing in our example domain.tld with your own domain name):

RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.domain.tld/$1 [R,L]

In our example, all requests made with a URL in http:// will be automatically rewritten to https://. Visitors will be redirected to your website by clicking on https://.

Go further

Tutorial - How do I block access to my website for certain IP addresses via a .htaccess file?

Protect your website's administration interface with a .htaccess file

For specialised services (SEO, development, etc.), contact OVHcloud partners.

If you would like assistance using and configuring your OVHcloud solutions, please refer to our support offers.

Join our community of users on https://community.ovh.com/en/.


Did you find this guide useful?

Please feel free to give any suggestions in order to improve this documentation.

Whether your feedback is about images, content, or structure, please share it, so that we can improve it together.

Your support requests will not be processed via this form. To do this, please use the "Create a ticket" form.

Thank you. Your feedback has been received.


These guides might also interest you...

OVHcloud Community

Access your community space. Ask questions, search for information, post content, and interact with other OVHcloud Community members.

Discuss with the OVHcloud community