简体   繁体   中英

How can i forward my www domain name to root domain

I do not have right to modify htaccess file. What can i do now to forward my www domain name to root domain.

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). Generally you'd do it all through .htaccess (or httpd.conf) with something like this:

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 .

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. On Apache, you'd probably be looking at configuration somewhat similar to the above in terms of complexity, plus the actual PHP redirect. Sending all requests to that PHP file would probably also break your website. 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 .

$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.

Beware: if your root domain redirects to www you'll cause an infinite loop.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM