Comments on: Forwarding sites & URL rewriting https://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/ A blog from the staff at NearlyFreeSpeech.NET. Sun, 19 Jul 2009 21:53:23 +0000 hourly 1 By: jdw https://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/#comment-23 Fri, 01 Dec 2006 21:37:23 +0000 http://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/#comment-23 I don’t think that your rewrite example will work, because you’ll only get a populated %1 only if your condition matches, which it won’t unless it doesn’t match www.(something), but the (something) implies the www.

Also, CNAMEs won’t work for this, because cannot create a CNAME on a bare domain name because a CNAME must be the only record for a given name. Bare domain names always have an SOA record, which rules out the use of CNAME.

It’s probably possible to do what you describe entirely in mod_rewrite, but there’s some kind of special psychological term for people who hurt themselves on purpose.

So I did it using a combo of mod_rewrite and PHP:

RewriteEngine on
RewriteRule ^.*$ /index.php

$strURL = "http://www.{$_SERVER['HTTP_HOST']}${_SERVER['REQUEST_URI']}";
header("Location: {$strURL}");
print "Redirecting to <a href=\"{$strURL}\">{$strURL}</a>";

You can use this functionality yourself, but I also liked the idea that we could just provide it since it’s so common. So, we’ve added an “Add Bare Domain Forward” option to our DNS management panel. It’ll appear if the bare domain name (e.g. example.com) is not in use and the www version (e.g. http://www.example.com) is.

I don’t see us doing the reverse, as it would eat another IP address and it’s far less common. But it’s still easy to do on your own, using any of the techniques described here.

Thanks for the feedback!

]]>
By: mojo https://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/#comment-19 Thu, 30 Nov 2006 18:28:08 +0000 http://blog.nearlyfreespeech.net/2006/11/17/forwarding-sites-url-rewriting/#comment-19 A third rather useful option is to use a regex in the mod_rewrite that takes any non-www host name and rewrites it with the www (or vice-versa, depending on personal preferences). Then point all of your non-www aliases at that site, and it will automatically forward to the www version. This saves you having 20 different redirect sites for 20 different real sites, and maintains the advantage of invoking mod_rewrite only when required.

A suitable expression would be:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+\..+)$ [NC]
RewriteRule ^.*$ http://www.%1%{REQUEST_URI} [R=301,L]

Given that this can be applied to all of your sites, one wonders if NFSN could make two sites that we could all CNAME to, one that adds “www” if required, and one that removes “www” if present, to match the most common of these cases. Then the really simple thing to suggest people do is point the alias that they want changing at the appropriate NFSN-wide site.

]]>