homepage Welcome to WebmasterWorld Guest from 54.227.11.45
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Redirect Questions in httpd.conf
Jessica97




msg:3506863
 4:17 pm on Nov 16, 2007 (gmt 0)

Have some redirect code that is working fine on a windows server using isapi rewrite and trying to recreate the same setup on Apache. Thanks for taking a look!

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]

 

jdMorgan




msg:3507076
 8:37 pm on Nov 16, 2007 (gmt 0)

A simpler and more efficient way to do this on Apache would be something like this:

# 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]

This rule should generally follow all other external redirects, in most-specific to least-specific order, and should precede all internal rewrites, again in most-specific to least-specific order.

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

Jessica97




msg:3508384
 2:11 am on Nov 19, 2007 (gmt 0)

Hi Jim, thanks so much for the help. I've tried all weekend to get the code you sent to work, but so far, can't seem to get the code working inside a virtual host container. (I was however, able to get some code working inside a virtual host container in the http.conf file )

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>

jdMorgan




msg:3508398
 2:35 am on Nov 19, 2007 (gmt 0)

In order to enable mod_rewrite, you'll need to add either
Options +FollowSymLinks
-or-
Options +SymLinksIfOwnerMatch

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved