php 将非 WWW 重定向到 WWW URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7228653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:17:30  来源:igfitidea点击:

Redirect Non-WWW to WWW URLs

phpapache.htaccessredirect

提问by Ullas Prabhakar

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

当人们访问我的域时,它会使用 php 代码重定向到http://www.mydomain.com/en/index.php。我 在 .htaccess 中添加了以下代码

RewriteEngine on
Options +FollowSymlinks

RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [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,

将人们从非 www重定向到 www

Still users can access by typing both http://mydomain.com/en/page-a1/abc.phpand http://www.mydomain.com/en/page-a1/abc.phpURLs

仍然用户可以通过输入http://mydomain.com/en/page-a1/abc.phphttp://www.mydomain.com/en/page-a1/abc.phpURL来访问

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

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

回答by genesis

$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

在 php 中

回答by Kokos

I'm not sure how to do it through .htaccess, but I do it with PHP code myself within my config.phpwhich is loaded for every file.

我不确定如何通过 .htaccess 执行此操作,但我自己在config.php为每个文件加载的PHP 代码中执行此操作。

if(substr($_SERVER['SERVER_NAME'],0,4) != "www." && $_SERVER['SERVER_NAME'] != 'localhost')
    header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);

EDIT:@genesis, you are correct I forgot about https

编辑:@genesis,你说得对,我忘记了 https

Change

改变

header('Location: http://www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);

to

header('Location: '.
       (@$_SERVER['HTTPS'] == 'on' ? 'https://' : 'http://').
       'www.'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);

回答by Arnaud Le Blanc

Add RewriteEngine Onbefore RewriteCondto enable your rewrite rules:

添加RewriteEngine OnbeforeRewriteCond以启用重写规则:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$  http://www.%{HTTP_HOST}/ [R=301,L]

And if you have https:

如果你有 https:

RewriteEngine On

RewriteRule .? - [E=PROTO:http]

RewriteCond %{HTTPS} =on
RewriteRule .? - [E=PROTO:https]

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$  %{ENV:PROTO}://www.%{HTTP_HOST}/ [R=301,L]

回答by Ashish

<?php
$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;
}
?>

Working Properly

好好工作

回答by Shef

Redirect 301 /pages/abc-123.html http://www.mydomain.com/en/page-a1/abc.php

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on

# mydomain.com -> www.mydomain.com
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/ [R=301,L]
</IfModule>

回答by Ravi

I think you want to redirect the user instead of rewriting the URL, in that case use Redirector 'RedirectMatch` directive. http://httpd.apache.org/docs/2.3/rewrite/remapping.html#old-to-new-extern

我认为您想重定向用户而不是重写 URL,在这种情况下使用Redirect或 'RedirectMatch` 指令。http://httpd.apache.org/docs/2.3/rewrite/remapping.html#old-to-new-extern