How to Redirect a Domain to a Directory Using .htaccess
On this page
If you want visitors to be automatically redirected to a specific directory within your website, you can achieve this using rules in the .htaccess file.
This can be useful when:
- Rebuilding your main website while keeping a section online.
- Redirecting visitors directly to an online store.
- Making a subdirectory the primary entry point of the website.
- Temporarily displaying content from a specific folder.
For example, visitors accessing:
https://yourdomain.com
can be automatically redirected to:
https://yourdomain.com/store
Before You Begin
The .htaccess file should be located in your website’s root directory.
By default, this is:
public_html
If the file does not exist, you can create it manually.
Redirect the Domain to a Directory
Add the following rules to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/store/
RewriteRule ^(.*)$ /store/$1 [L]
Replace:
yourdomain.com
with your actual domain name, and:
/store/
with the directory to which you want visitors redirected.
Example
To redirect all traffic from:
https://mywebsite.com
to:
https://mywebsite.com/shop
use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/shop/
RewriteRule ^(.*)$ /shop/$1 [L]
How the Rule Works
The configuration:
- Checks whether the request is for the specified domain.
- Verifies that the visitor is not already inside the target directory.
- Redirects all requests into the selected directory.
- Prevents redirect loops.
Without the second condition:
RewriteCond %{REQUEST_URI} !^/store/
the website could continuously redirect itself.
Important Notes
- Ensure the target directory already exists.
- Verify that the directory contains a valid website or index file.
- Back up your existing
.htaccessfile before making changes. - Test the redirect after saving the file.
Troubleshooting
If the redirect does not work:
- Confirm the
.htaccessfile is located in the correct directory. - Check that the directory name is correct.
- Clear your browser cache.
- Verify there are no conflicting redirect rules in the file.
Summary
To redirect all visitors from a domain to a specific directory, place the following rule in the .htaccess file located in your website’s public_html directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/store/
RewriteRule ^(.*)$ /store/$1 [L]
After saving the file, visitors accessing the domain will automatically be directed to the selected directory within the website.
Didn't find an answer?
Our support team is here for you 24/7 – write to us and we will help you sort it out.