简体   繁体   中英

Redirect secured domain to secured subdomain using htaccess

I have 3 domains and one hosting. I am trying to use the same hosting for all of my three domains through htaccess. So, I created a subdomains with that name on my domain linked with hosting, which looks something like below.

www.site1.com [Main domain linked with hosting]

site2.site1.com [subdomain for www.site2.com ]

site3.site1.com [subdomain for www.site3.com ]

What I want to achieve is, user shouldn't go to subdomain site2.site1.com , instead they would be able to go to www.site2.com only and request will be sent to site2.site1.com at backend.

Up to here, all is done and worked well. The only problem comes afterwards, when I adds SSL on site. I have SSL for all of these domain and subdomain. If a user visit non-ssl, then he should be redirected to SSL one. Some of SSL works well but when I add SSL for all of them, then I start getting 500 error.

Here is my .htaccess file

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^site2.com
RewriteRule ^(.*) https://site2.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^(.*) https://site2.site1.com/$1 [P]

RewriteCond %{HTTP_HOST} ^site3.com
RewriteRule ^(.*) https://site3.site1.com/$1 [P]
RewriteCond %{HTTP_HOST} ^www.site3.com
RewriteRule ^(.*) https://site3.site1.com/$1 [P]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

As I add https on redirection link, so it start giving me 500 error whereas when I make redirection to http then it will not load my page because non-secured site http://site2.site1.com will be loaded over secured https://www.site2.com and in a result, nothing will be shown.

Here I need help to resolve this problem. I have looked over different questions but haven't found any question relevant to me because I need to keep my .htaccess working with redirection and SSL . Moreover, I also need to redirect to www one, if not added in URL.

Any help will be appreciated.

Like, all the times I experienced here I have resolved my problem myself. The resolution to my problem was the following htaccess

RewriteEngine On
DirectoryIndex index.php

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} ^site2.com
RewriteRule ^(.*) site2/$1
RewriteCond %{HTTP_HOST} ^www.site2.com
RewriteRule ^(.*) site2/$1

RewriteCond %{HTTP_HOST} ^site3.com
RewriteRule ^(.*) site3/$1
RewriteCond %{HTTP_HOST} ^www.site3.com
RewriteRule ^(.*) site3/$1

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

In this way instead of pointing the URL to subdomain having https I have reference that to the directory instead because path will be same for that, and now when I enter the URL with https page opens and don't give any error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM