Tutorial - Rewrite the URL for accessing your website using mod_rewrite via the .htaccess file
Find out how to 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.
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
Information regarding OVHcloud service administration and how to find appropriate assistance
When using OVHcloud guides, please be aware of the following conditions:
- User instructions aim to provide as many details as possible but cannot cover individual use cases. You might need to adapt the pertinent actions to your requirements.
- The OVHcloud ecosystem is built for flexibility and freedom of choice. Customers are therefore responsible for the secure and proper configuration of their services. To prevent data loss, we strongly recommend to apply backup strategies to all your important data.
- Our guides and tutorials may reference third-party software or services in combination with OVHcloud solutions. The technical support provided by OVHcloud does not include the configuration of systems or products outside of our responsibility. This includes but is not limited to:
- Operating systems and user interfaces (Windows, Debian, Plesk, etc.).
- Any other third-party software (FTP clients, email software, etc.).
- Services offered by other providers (DNS, APIs, user interfaces, etc.).
To receive the appropriate assistance for any issues you might experience, follow these guidelines:
- You seek personalized advice or you would like to discuss a topic that is not covered in detail by our documentation?
Join the OVHcloud Community to search for your topic and reach out to other users. - You need to report an incident regarding your OVHcloud service or you are experiencing difficulties in the OVHcloud Control Panel?
Create a support request in our Help Centre. - You require professional assistance for your project or you need help with tasks outside our support scope?
Visit our partner portal to search for experts who are familiar with OVHcloud solutions. - You are looking for more detailed information regarding our support levels and Professional Services?
Please visit our web pages for OVHcloud support levels and OVHcloud Professional Services.
You can participate in improving our documentation:
- You would like to share feedback to improve a guide page or you want to report insufficient information on a specific page?
Use the "Was this page helpful?" buttons at the bottom of the page to let us know. - You would like to propose a specific documentation update?
Use the "Edit this page" function, available at the bottom of the page and in the sidebar.
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):
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):
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):
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" 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):
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):
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.