SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Skåne, Sweden.
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    http://www redirect to http://

    Hey,
    I'd like to configure my site to redirect to http://example.com when http://www.example.com is accessed.. How is this accomplished? I have control of subdomains and the DNS zone, but not the server config itself.
    Thanks!

  2. #2
    Idler. Chazzy's Avatar
    Join Date
    Jan 2004
    Location
    Uk
    Posts
    336
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Set up a forwarder for www.example.com to http://example.com in the domain configuration menu. You shouldnt need to alter dns zones etc just adding a forwarder ... (a CNAME) even.

  3. #3
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Skåne, Sweden.
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Chazzy
    Set up a forwarder for www.example.com to http://example.com in the domain configuration menu. You shouldnt need to alter dns zones etc just adding a forwarder ... (a CNAME) even.
    Yeah, that's exactly what I want to do, but I don't know how. What domain configuration menu are you talking about? I have access to CPanel and WHM... I have little experience in this, so please have patience with me

    Edit: Also, I need to use a method that Google doesn't penalize...

  4. #4
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, well, www.example.com is usually CNAME to example.com. To the end user it works as the same but in fact it is just an "alias" of the real domain.
    Is there any special reason you would not want users to view the site with the www. before?
    Anyhow, if so, you could propably create a index.php file or something that looks like this:
    PHP Code:
    <?php

    header
    ('HTTP/1.1 301 Moved·Permanently');
    header('Location: http://example.com/');
    header('Connection: close');
    header('Content-Type: text/html; charset=iso-8859-1');

    ?>
    <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
    <html><head>
    <title>301 Moved Permanently</title>
    </head><body>
    <h1>Moved Permanently</h1>
    <p>The document has moved <a href="http://example.com/">here</a>.</p>
    </body></html>
    I'm not quite sure how this headers work though. I always though you only needed to send the new URI and the browser would redirect automatically and then you wouldn't need the Location header. Anyone that knows that and is willing to share his knowledge?
    Edit:

    Solved, the URI header isn't sent (at least apache doesn't). Location is therefore the only header that "moves" ther user. Script changed accordingly...
    - website

  5. #5
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Skåne, Sweden.
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by website
    Is there any special reason you would not want users to view the site with the www. before?
    I simply want to make the address shorter, and personally I feel that www is getting a bit old I would like it to work like http://www.wordpress.org/ does for example, where you're redirected(or forwarded?) to the unprefixed address.
    Quote Originally Posted by website
    Anyhow, if so, you could propably create a index.php file or something that looks like this:
    PHP Code:
    <?php

    header
    ('HTTP/1.1 301 Moved·Permanently');
    header('URI: http://example.com');
    header('Location: http://example.com');
    header('Connection: close');

    ?>
    Yeah, I thought about that, but www.example.com and example.com point to the same directory, and using the above as an index file, would start a never ending loop... I've tried to actually create a subdomain called "www", and then stick a file like that in that directory, but I couldn't create the subdomain, it was reserved or something like that. I tried removing the CNAME in the DNS zone first and all, but it didn't work(put it back in btw). I really don't know much about this stuff.

  6. #6
    ********* Member website's Avatar
    Join Date
    Oct 2002
    Location
    Iceland
    Posts
    1,238
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, I noticed that www.wordpress.org uses exactly that method, or at least, they send the headers I showed as edited above. They propably don't use a .php file to output it though .
    I recon there is something in the Apache settings (eg virtualhost) that tells it to redirect all users to the domain without the www. I don't know how that is done though...
    - website

  7. #7
    FreeBSD The Power to Serve silver trophy pippo's Avatar
    Join Date
    Jul 2001
    Location
    Italy
    Posts
    4,514
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]

    Place it inside an .htaccess or inside the VirtualHost of example.com (The VirtualHost is approach is better, but you should restart apache then)

    Mr Andrea
    Former Hosting Team Advisor
    Former Advisor of '03

  8. #8
    SitePoint Member
    Join Date
    Oct 2003
    Location
    Skåne, Sweden.
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pippo
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]

    Place it inside an .htaccess or inside the VirtualHost of example.com (The VirtualHost is approach is better, but you should restart apache then)

    Thanks a lot, pippo! I have no possibility to restart Apache, or change the configs, but .htaccess did the job nicely.

    If anybody has any idea on how to do this by playing with the DNS zone or something like that, please tell, because even if mod_rewrite works fine, it's maybe not the *optimal* solution?

    Thanks again, ciao


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •