Discover how to redirect a subdomain to a specific subfolder using `.htaccess`. Follow our detailed guide to enforce HTTPS, non-www, and manage your website's structure effectively.
---
This video is based on the question https://stackoverflow.com/q/67704806/ asked by the user 'Ole' ( https://stackoverflow.com/u/10970877/ ) and on the answer https://stackoverflow.com/a/67705154/ provided by the user 'RavinderSingh13' ( https://stackoverflow.com/u/5866580/ ) at 'Stack Overflow' website. Thanks to these great users and Stackexchange community for their contributions.
Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: RewriteRule for subdomain to subfolder .htaccess
Also, Content (except music) licensed under CC BY-SA https://meta.stackexchange.com/help/l...
The original Question post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license, and the original Answer post is licensed under the 'CC BY-SA 4.0' ( https://creativecommons.org/licenses/... ) license.
If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Redirecting a Subdomain to a Subfolder in .htaccess
In the world of web development, configuring your server settings accurately can often be a daunting task. One common challenge developers face is setting up redirection rules using .htaccess. If you're trying to have a different subfolder load for a specific subdomain, you're not alone. Today, we'll discuss how to restructure your .htaccess file to ensure that test.example.com loads content from /test, while also managing other important configurations like HTTPS and non-www redirection.
Understanding the Requirements
In the initial setup, you had several configurations that were working perfectly:
Non-HTTP traffic was successfully forced to HTTPS.
The website successfully redirected from www to non-www.
Your website was loading from the /web subfolder.
However, you faced issues when trying to redirect test.example.com to the /test subfolder. The existing rules in your .htaccess weren't sufficient for this specific requirement. Below, we'll present a comprehensive solution.
Solution: Revised .htaccess Rules
To achieve the desired functionality, consider using the following .htaccess rules:
[[See Video to Reveal this Text or Code Snippet]]
Breakdown of the Rules
HTTPS Redirection: The first block ensures that any incoming requests that are not secure (HTTP) are redirected to HTTPS. This is critical for user security and SEO.
RewriteCond %{HTTPS} !on: Checks if HTTPS is not enabled.
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]: Redirects to HTTPS while preserving the requested URI.
Non-WWW Redirection: The second block handles the redirection of the www version of your site to the non-www version.
RewriteCond %{HTTP_HOST} ^(?:www.)(example.com)$ [NC]: Conditions for matching www.example.com.
RewriteRule ^ https://%1%{REQUEST_URI} [NE,L,R=301]: Redirects to the non-www version, maintaining the request URI.
Loading Content from /web: The next section ensures that general requests to example.com load the /web subfolder unless specified to load /test.
RewriteCond %{HTTP_HOST} ^example.com$ [NC]: Matches requests to example.com.
RewriteCond %{REQUEST_URI} !^/test [NC]: Avoids a conflict with the /test subfolder.
RewriteRule ^(.*)/?$ example/web/$1 [L,NC,QSA]: Serves files from the /web subfolder.
Redirecting the Subdomain to the /test Folder: Finally, the last section specifies the redirection rules for test.example.com to ensure it loads content from the /test subfolder.
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]: Matches requests to the subdomain.
RewriteRule ^(.*)/?$ www.example.com/test/$1 [L,NC,QSA]: Redirects to the subfolder, ensuring that all related requests are handled properly.
Testing Your Configuration
After implementing these changes, it’s essential to test your configuration to ensure everything functions as expected. Here are a few tips:
Clear Browser Cache: Browsers often cache redirects, so clear your cache before testing the new rules.
Use Tools to Test Redirects: Utilize tools like HTTP status checkers to ensure the redirects work correctly.
Conclusion
Redirecting subdomains to specific subfolders can be confusing, but with the right rules in your .htaccess file, it can be accomplished smoothly. By following the guide above, you ensure that your web applications run securely and efficiently. Enjoy seamless navigation between your primary site and its subdomains!
Информация по комментариям в разработке