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

余生颓废 提交于 2019-12-11 05:09:11

问题


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.


回答1:


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]


来源:https://stackoverflow.com/questions/47873287/php-redirect-main-domain-to-https-www-subdomain-to-https-without-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!