PHP:将主域重定向到https:// www,子域重定向到https://(不带www) [英] PHP: Redirect main domain to https://www, subdomain to https:// (without www)

查看:183
本文介绍了PHP:将主域重定向到https:// www,子域重定向到https://(不带www)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想修改 php -file以满足以下需求:

I want to modify a php-file to fulfill the following needs:

主域名:


  • http 重定向到 https

  • 非www 重定向至 www

  • (对于主域名, www 是理想的,应该出现)

  • Redirect http to https
  • Redirect non-www to www
  • (for main domain, www is desired and should appear)

子域名


  • 重定向 http https

  • www 重定向至非www

  • (对于子域名, www 不是必需的,不应出现)

  • Redirect http to https
  • Redirect www to non-www
  • (for subdomains, www is not desired and should not appear)

评论:子域名内容完全不同比主域(可能是 private.website.com cloud.website.com )。 只是一种不同的语言。因此,通过浏览器或CMS创建内部转发似乎没有用。它应该在最好的情况下通过 .hataccess 执行。

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.

推荐答案

如果你正在使用PHP然后你需要写一些条件你没有给出子域的任何例子如果你要使用像de.example.com,uk.example.com这样的子域,那么你需要在帮助下重定向浏览器语言或者您也可以在访问者位置的帮助下进行

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

就像

<?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到https

1) http to https

RewriteCond %{HTTPS} !=on

2)非www到www

2) non-www to www

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

3)www到非www

3) www to non-www

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

这篇关于PHP:将主域重定向到https:// www,子域重定向到https://(不带www)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆