Forum Moderators: phranque
1. Is there some way of putting in the blanket redirection one time in the http.conf file in Apache so that all .com, .net. and .org domains are always redirected to the www.sitename.com version of the site without having to either go in individually in a virtual host container each time a new domain is added, or manually adding to htaccess? If so, where does this go in Apache's http.conf file? Can one setup a default virtual host container for rules such as this and if so, any chance of getting a sample of the syntax? I'm trying to come up with a better solution to set all redirects to the www.sitename.com version of any domains, unless it is a subdomain ie: subdomain.sitename.com (On the windows server I have it working great, could someone help me with how to accomplish this in Apache?)
Here is my redirect rule that works great on the windows server (using isapi rewrite I think the syntax is supposed to be compatible with apache) I've only done the .com version below, but it would be nice to know how to add any .net or .org extensions as well.
Windows Sample: this one works nicely...
RewriteEngine on
RewriteCond %{HTTPS} (on)?
RewriteCond %{HTTP:Host} ^(?!www\.)([^.]+\.com)$ [NC]
RewriteCond %{REQUEST_URI} (.+)
RewriteRule . http(?%1s)://www.%2%3 [R=301]
# if domain.tld only (no subdomain)
RewriteCond %{HTTP:HOST} ^[^.]+\.[^.]+$
# create "s" back-reference if HTTPS port
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
# externally redirect to www subdomain preserving http/https and domain
RewriteRule / http(%2)://www.%{HTTP_HOST} [R=301,L]
Change the broken pipe "¦" character in the RewriteCond to a solid pipe before use: Posting in this forum modifies the pipe characters.
The default server container on Apache is only used if no other defined name-based server is resolved, so it isn't much use in this case. You can try placing this code outside of the <ServerName> containers after getting it to work inside one server -- Frankly, I can't remember if that works or not.
Jim
Here is my code that works, (Obviously, it doesn't do everything I wanted like ignoring the subdomains, but at least I was able to get the http.conf file working whereas before I could only get redirects working using htaccess, so this is progress.
Here is what's in the http.conf file: (I tried taking out my rewrite conditions and putting your sample code in and did remove the broken pipe from the code. When using your sample, the domain does not redirect when called using the non www, but it does work ok, using the code below. Any suggestions? I'm learning but am not well versed at this at all.
<VirtualHost 00.00.00.000>
ServerAlias www.example.com
ServerAdmin webmaster@example.com
DocumentRoot /home/example/public_html
ServerName example.com
User example
Group example
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.example\.com
RewriteRule (.*) http://www.example.com$1 [R=301,L]
BytesLog /usr/local/apache/domlogs/example.com-bytes_log
CustomLog /usr/local/apache/domlogs/example.com combined
ScriptAlias /cgi-bin/ /home/northcar/public_html/cgi-bin/
</VirtualHost>
Note that the "+" means that these options should be added to those already in effect. If you haven't previously set any other Options, then the "+" is entirely optional. See Options in Apache core docs for more info.
I'm not understanding what you mean by trying to get this code to work inside a vHost container, but not within httpd.conf. You can't use a vHost container in .htaccess, so where else were you trying to get that to work?
Jim