我已经在共享主机上写了一个简单的PHP脚本,并希望在.htaccess文件中实现一些规则,这样每次我的脚本调用http://www.google.com/test1时,都会得到http://www.otherwebsite.com/test1。
我以前使用过标准的URL重写规则,但不需要这个特定的函数。
谢谢!
发布于 2013-08-20 03:25:35
如果我正确理解您的问题,您希望将所有URL重定向到其他域。
通过httpd.conf启用mod_rewrite和.htaccess,然后将以下代码放入DOCUMENT_ROOT目录下的.htaccess中:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?yourwebsite\.com$ [NC]
RewriteRule ^ http://www.otherwebsite.com%{REQUEST_URI} [R=301,L]发布于 2013-08-20 07:04:16
.htaccess只能重写带有解析到您的web服务器的域名的传入URL。它无法控制传出的URL,因为这些请求直接发送到传出的web服务器(在您的示例中为google.com)。
您可能需要的是一个脚本解决方案,它可以通过连接到所有外部链接的onclick事件来根据您的需要重定向用户。下面是对jQuery概念的快速证明,但也可以使用标准JavaScript来完成。
<html>
<head>
<title>jQuery global redirector</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
</head>
<body>
<a href="http://google.com/search?q=jquery">google url would redirect</a><br />
<a href="http://mysite.com/somepage.php">mysite.com url won't redirect</a>
<script>
<!--
$(function() {
$("a").click(function(e) {
var url = e.target.href;
if(!(url.startsWith("mysite.com") ||
url.startsWith("http://mysite.com"))) {
var path = $( '<a />', {href : url} ).prop( 'pathname' );
window.location.href = "http://otherwebsite.com" + path;
e.preventDefault();
}
});
});
//-->
</script>
</body>
</html>您可能应该将脚本放在另一个文件(比如redirect.js)中,然后在需要重定向的页面中选择性地包含此脚本。另外,也不要忘记导入jQuery。
https://stackoverflow.com/questions/18316573
复制相似问题