Dave Dave - 9 months ago 75
Apache Configuration Question

PHP: Redirect main domain to https://www, subdomain to https:// (without www)

I want to modify a

php
-file to fulfill the following needs:

Main domain:


  • Redirect
    http
    to
    https

  • Redirect
    non-www
    to
    www

  • (for main domain,
    www
    is desired and should appear)



Subdomains:


  • Redirect
    http
    to
    https

  • Redirect
    www
    to
    non-www

  • (for subdomains,
    www
    is not desired and should not appear)






Comment: The subdomain has a completely different content than the main domain (could be private.website.com or cloud.website.com for example). It is not just a different language. Because of this, it seems not useful to create a internal forwarding via browser or CMS. It should be executed via
.hataccess
in best case.

Answer Source

If you are using php then you need to write some conditions And you didn't give any example of sub domain IF you are going to use sub domain like de.example.com , uk.example.com then you need to redirect with the help of browser language Or you can also do it with the help of visitors locations

just like

<?php    
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);

if($lang=="de")
{

header('Location: https://de.example.com/');

}    
?>

IN that case you need to do the followings.

1) http to https

RewriteCond %{HTTPS} !=on

2) non-www to www

RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L] 

3) www to non-www

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download