我已经编写了一个关于共享托管的简单PHP脚本,并希望在.htaccess文件中实现一些规则,这样每当我的脚本调用时,http://www.google.com/test1它会得到http://www.otherwebsite.com/test1相反
我以前使用过标准的URL重写规则,但不需要这个特定的函数。
谢谢!
如果我正确理解您的问题,您希望将所有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]
.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。