重定向非www到WWW网址 [英] Redirect Non-WWW to WWW URLs

查看:128
本文介绍了重定向非www到WWW网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当人们访问我的域名被重定向至 http://www.mydomain.com/en/index.php 使用PHP codeI增加了以下code在.htaccess

  RewriteEngine叙述上
选项​​+的FollowSymLinks

的RewriteBase /
的RewriteCond%{HTTP_HOST}!^ WWW \。
(。*)重写规则^ $ HTTP://www.% {HTTP_HOST} / $ 1 [R = 301,L]
永久重定向/pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php
 

重定向人的非WWW到www

不过用户可以通过键入这两个 http://mydomain.com/en/page-a1/abc访问。 PHP http://www.mydomain.com/en/page-a1/abc。 PHP 网址

有谁知道方法完全重定向到 http://www.mydomain.com/en/页A1 / abc.php 即使用户输入 http://www.mydomain.com /en/page-a1/abc.php ,并禁止访问的URL加www。

解决方案

  $ PROTOCOL =(@ $ _ SERVER [HTTPS] ==上)? https://开头:HTTP://;

如果(SUBSTR($ _ SERVER ['HTTP_HOST'],0,4)!=='WWW'){
    头('位置:''。$ _ SERVER $ protocol.'www'。$ _ SERVER ['HTTP_HOST']。'/ ['REQUEST_URI']);
    出口;
}
 

在PHP

When people access my domain it is redirected to http://www.mydomain.com/en/index.php using php code.I added the following code in .htaccess

RewriteEngine on
Options +FollowSymlinks

RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RedirectPermanent /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php

to redirect people from non www to www,

Still users can access by typing both http://mydomain.com/en/page-a1/abc.php and http://www.mydomain.com/en/page-a1/abc.php URLs

Does anyone know the method to completely redirect to http://www.mydomain.com/en/page-a1/abc.php even if user typed http://www.mydomain.com/en/page-a1/abc.php, and prohibit accessing URLs without www.

解决方案

$protocol = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";

if (substr($_SERVER['HTTP_HOST'], 0, 4) !== 'www.') {
    header('Location: '.$protocol.'www.'.$_SERVER['HTTP_HOST'].'/'.$_SERVER['REQUEST_URI']);
    exit;
}

in php

这篇关于重定向非www到WWW网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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