---
title: "Tutorial - Rewrite the URL for accessing your website using mod_rewrite via the .htaccess file"
description: "Find out how to rewrite the URL for accessing your website using mod_rewrite via the .htaccess file"
url: https://docs.ovhcloud.com/en/guides/web-cloud/web-hosting/htaccess-url-rewriting-using-mod-rewrite
lang: en
lastUpdated: 2026-06-16
---
# Tutorial - Rewrite the URL for accessing your website using mod_rewrite via the .htaccess file

  

## 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.

:::tip
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](https://httpd.apache.org/docs/2.4/en/mod/mod_rewrite.html).

:::

**This tutorial explains how to rewrite the URL for accessing your website using mod\_rewrite via the .htaccess file**
  

## Requirements

- An [OVHcloud Web Hosting plan](https://www.ovhcloud.com/en-gb/web-hosting/)
    

## Instructions

:::warning
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](https://partner.ovhcloud.com/en-gb/directory/) if you encounter any difficulties. We will not be able to assist you. You can find more information in the [Go further](#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](/en/guides/web-cloud/web-hosting/ftp-connection.md).

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):

```bash
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):

```bash
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):

```bash
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 using an OVHcloud shared hosting plan, your domain name (e.g., **domain.tld**) is declared on a website present on your web hosting. This is to display the content of a target folder also known as the `root folder`. This is the folder that contains the files of your website. You can customise the name of this `root folder` when you add the website to your hosting via the OVHcloud Control Panel.

Refer to our guide "[Hosting multiple websites on your Web Hosting plan](/en/guides/web-cloud/web-hosting/multisites-configure-multisite.md)" if you want to find out more about this topic.

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 the files of your website are not directly in the `root folder` declared for your website in the OVHcloud Control Panel and you do not want to display the folder name in the URL of your website, edit the ".htaccess" file located at the root of the directory containing 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):

```bash
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](/en/guides/web-cloud/web-hosting/ssl-on-webhosting.md).

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):

```bash
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?](/en/guides/web-cloud/web-hosting/htaccess-how-to-block-a-specific-ip-address-from-accessing-your-website.md)

[Protect your website's administration interface with a .htaccess file](/en/guides/web-cloud/web-hosting/htaccess-protect-directory-by-password.md)

For specialised services (SEO, development, etc.), contact [OVHcloud partners](https://partner.ovhcloud.com/en-gb/directory/).

If you would like assistance using and configuring your OVHcloud solutions, please refer to our [support offers](https://www.ovhcloud.com/en-gb/support-levels/).

Join our [community of users](https://community.ovhcloud.com/).
