Handling subdomains and https with .htaccess
Question
In the .htaccess example below, if someone types in a URL like the following...
http://mysite.com/ricks-motorcycles
...it will automatically load the page from x.com's subdirectory under public_html called "ricks-motorcycles". This technique is called Proxy Throughput.
RewriteEngine On
RewriteRule ^ricks-motorcycles/(.*)$ http://x.com/ricks-motorcycles/$1 [P,L]
This is great, but how do I handle two other situations:
(1) Someone wanting https instead of http.
(2) Someone wanting...
http#//ricks-motorcycles.mysite.com/
...instead of...
http#//mysite.com/ricks-motorcycles/
(Switch # with : above because StackOverflow was blocking me from posting.)
Solution
You can qualify your rewrites with a RewriteCond
:
RewriteEngine On
RewriteCond %{HTTPS} =on
RewriteRule ^ricks-motorcycles/(.*)$ https://example.com/ricks-motorcycles/$1 [P,L]
RewriteCond %{HTTP_HOST} =ricks-motorcycles.mysite.com
RewriteRule ^(.*)$ http://example.com/ricks-motorcycles/$1 [P,L]
For more information, see the mod_rewrite documentation.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow