1

我无权修改 htaccess 文件。我现在可以做些什么来将我的 www 域名转发到根域。

前任:

www.bulkanswer.com to bulkanswer.com
4

3 回答 3

1

为了将所有请求重定向www.example.comexample.com,您需要进行一定数量的服务器配置(例如,.htaccess 修改)。通常,您会通过 .htaccess(或 httpd.conf)使用以下内容完成所有操作:

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

那将重定向http://www.example.com/anythinghttp://example.com/anything.

使用 PHP 可以做到这一点,但为了做到这一点,您必须将服务器配置为将所有请求发送到给定的 PHP 文件,该文件将处理重定向。在 Apache 上,您可能会看到在复杂性方面与上面有些相似的配置,以及实际的 PHP 重定向。将所有请求发送到该 PHP 文件也可能会破坏您的网站。尽管如此,除非您的网站目前是这样设置的(我非常怀疑),否则很难重定向除您的索引页面等以外的任何内容。

于 2012-06-26T23:21:12.943 回答
0

You can do this in PHP, if you must, but if at all possible the htaccess route is much better. Here is some code which should do what you're after:

// Check if the current request has www in the server name
if (substr($_SERVER['SERVER_NAME'], 0, 3) === "www") {
   // Redirect required so ensure you use a 301
   header("HTTP/1.1 301 Moved Permanently");
   header("Location: http://example.com" . $_SERVER['REQUEST_URI']);
}

It's very important you use the 301 so that the redirect is cached in the browser and you don't cause any issues by having competing domains with regards to your sites SEO.

A few notes: there may be a better way than using the REQUEST_URI depending on how your site is created, but this is a pretty simple solution that should work in most instances. There may be issues with sending forms that are then redirected (can't remember off the top of my head) so make sure all links in your own HTML are always pointing to the non-www version.

于 2012-06-26T23:23:25.953 回答
0

如果你可以使用 PHP,你可以设置一个location header

$url = 'http://yourdomain.com';
header( 'Location: ' . $url );

您希望在通过例如$_SERVERvar 检查非 www 的条件中执行此操作。

注意:如果您的根域重定向到 www,您将导致无限循环。

于 2012-06-26T22:39:18.687 回答