How to redirect your root directory to a subdirectory?
To redirect your root directory to a subdirectory using the .htaccess
file, you can use the following code. This is useful when you want visitors to access your site through a specific subdirectory rather than the root.
Assuming your site is located in a subdirectory named “subdirectory,” here’s the .htaccess
code to redirect:
apache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule ^(.*)$ /subdirectory/$1 [L]
</IfModule>
Explanation of the code:
RewriteEngine On
: Enables the Apache mod_rewrite engine.RewriteCond %{REQUEST_URI} !^/subdirectory/
: Checks if the request is not already for the subdirectory.RewriteRule ^(.*)$ /subdirectory/$1 [L]
: Redirects all requests to the subdirectory while preserving the rest of the URL.
Replace “subdirectory” with the actual name of your subdirectory. Place this code in the .htaccess
file located in your website’s root directory.
Important Notes:
- Make sure mod_rewrite is enabled on your Apache server.
- Always make a backup of your
.htaccess
file before making changes. - If you have an existing
.htaccess
file, be careful not to overwrite any existing rules.
After applying these changes, accessing your site’s root URL (e.g., http://yourdomain.com
) will automatically redirect to the specified subdirectory (e.g., http://yourdomain.com/subdirectory
). Adjust the code based on your specific subdirectory name and requirements.