简体   繁体   English

我如何将我的www域名转发到根域

[英]How can i forward my www domain name to root domain

I do not have right to modify htaccess file. 我没有修改htaccess文件的权利。 What can i do now to forward my www domain name to root domain. 现在我该怎么做才能将我的www域名转发到根域。

Ex: 例如:

www.bulkanswer.com to bulkanswer.com

In order to redirect all requests to www.example.com to example.com , you'd need to do a certain amount of server configuration (eg, .htaccess modification). 为了将所有对www.example.com请求重定向到example.com ,您需要进行一定数量的服务器配置(例如,.htaccess修改)。 Generally you'd do it all through .htaccess (or httpd.conf) with something like this: 通常,您可以通过.htaccess(或httpd.conf)使用以下方式完成所有操作:

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

That would redirect http://www.example.com/anything to http://example.com/anything . 这样会将http://www.example.com/anything重定向到http://example.com/anything

It's possible to do this with PHP, but in order to do that, you'd have to have your server configured to send all requests to a given PHP file, which would handle the redirect. 可以使用PHP进行此操作,但要这样做,您必须将服务器配置为将所有请求发送到给定的PHP文件,该文件将处理重定向。 On Apache, you'd probably be looking at configuration somewhat similar to the above in terms of complexity, plus the actual PHP redirect. 在Apache上,您可能会考虑在复杂性以及实际的PHP重定向方面与上述配置有些相似的配置。 Sending all requests to that PHP file would probably also break your website. 将所有请求发送到该PHP文件也可能会破坏您的网站。 Still, unless your website is currently set up like this (which I doubt very seriously), it's going to be hard to redirect anything other than your index page, etc. 不过,除非您的网站目前的设置是这样的(我对此非常怀疑),否则将很难重定向除索引页面等以外的任何内容。

If you can use PHP, you can set a location header . 如果可以使用PHP,则可以设置location 标头

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

You'd want to do this inside a conditional that checks for non-www via, for example, the $_SERVER var. 您想在通过例如$_SERVER var检查非www的条件中执行此操作。

Beware: if your root domain redirects to www you'll cause an infinite loop. 注意:如果您的根域重定向到www,则会导致无限循环。

You can do this in PHP, if you must, but if at all possible the htaccess route is much better. 可以根据需要在PHP中执行此操作,但是htaccess路由要好得多。 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. 使用301非常重要,这样重定向便会存储在浏览器中,并且不会因与您的网站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. 一些注意事项:根据网站的创建方式,可能有比使用REQUEST_URI更好的方法,但这是一个非常简单的解决方案,在大多数情况下都可以使用。 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. 发送表单然后重定向后可能会出现问题(我不记得了),因此请确保您自己的HTML中的所有链接始终指向非www版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在没有http的情况下将用户重定向到PHP中的www域? - How can I redirect a user to a www domain in PHP without the http? 如何让我的 WordPress 网站在成功登录后重定向到子域,它们将重定向回根域 - How can I make my WordPress site redirect to subdomain once successfully login they will redirect back to root domain 如何将非www重定向到我的域中的www - How to redirect non-www to www in my domain PHP路由:如何将www.domain.com/download路由到与域根文件夹相同级别的文件夹? - PHP Routing: How to route www.domain.com/download to a folder that is same level with the domain root folder? 我可以使用PHPMailer接收有关我的域名的电子邮件吗? - Can I use PHPMailer for receiving emails on my domain name 我的域名未将www重定向到非www - My domain not redirect www to non- www 如何在文件调用中引用本地主机或域名? - How do I refer to localhost or my domain name in calls to files? 从php服务器获取根DNS条目; 获取没有www等的域名 - Get root DNS entry from php server; get domain name without www, ect 不使用域名的服务器根目录中的路径 - Path from root on my server without using domain name 我无法将magento域重定向到HTTPS并添加www - I can't redirect magento domain to HTTPS and add www
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM