[英]redirect www.domain.com/example.com to example.com
许多年前,我读到了一个简单的PHP脚本,它将您的网站(例如http://example.com/google.com
重定向到google.com
,并且适用于斜杠右边的任何域。 我忘记了这个脚本是什么或在哪里可以找到它
如果您在文档根目录中创建htaccess文件,并添加以下内容:
RewriteEngine On
RewriteRule ^/?([a-z0-9-.]+)$ http://$1/ [L,R]
如果要永久重定向,请使用301重定向。 这是搜索引擎安全的,智能浏览器可以使用此功能升级书签。
例如http://www.phpjunkyard.com/tutorials/php-redirect.php
<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.New-Website.com");
?>
编辑 (回复下面的评论)
这篇文章(http://stackoverflow.com/questions/8775374/how-to-get-everything-after-the-domain-name-into-a-string)将有助于建立重定向到的域。 如果我们将其构建到诸如get_url()
类的函数中,则可以将以上内容更改为
<?php
my_url = get_url();
// CHECK IT IS A SAFE URL
// REDIRECT
header("HTTP/1.1 301 Moved Permanently");
header("Location: " . my_url);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.