Forum Moderators: phranque

Message Too Old, No Replies

Apache Virtual Hosting www.example.com vs. example.com

www.example.com vs. example.com

         

eddie111

12:06 am on Feb 7, 2006 (gmt 0)

10+ Year Member



Hi

My sites are hosted on Apache virtual hosting server, my add-on sites are getting listed in SE's as http://example.com, what i am looking for, is making modifications in .htaccess file so they appear as http://www.example.com

I read about it and found that i have to use variations of following code, but i am not able to figure it out.

# And for a site running on port 80
RewriteCond %{HTTP_HOST}!^fully\.qualified\.domain\.name [NC]
RewriteCond %{HTTP_HOST}!^$
RewriteRule ^/(.*) [fully.qualified.domain.name...] [L,R]

jdMorgan

12:14 am on Feb 7, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code you posted was for use in httpd.conf. It changes slightly (along with several minor corrections) for use in .htacess:

# If HTTP_HOST request header is non-blank
RewriteCond %{HTTP_HOST} .
# AND HTTP_HOST request header does NOT contain canonical (preferred) domain name
RewriteCond %{HTTP_HOST} !^www\.example\.com
# Then issue 301-moved permanently to same 'page' on canonical domain
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

Jim

Pfui

7:33 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Jim, I tried the preceding code in .htaccess and alas, for all files 'in' directories (/public_html/directory/file.html), it always redirects to my home page, not to the same 'page'/URL.

The only time the redirects to home do NOT happen is when the desired file is top-level (/public_html/file.html).

I tried the numeric equivalent with the same results: top-level->A-OK; directory-level(s)->home:

RewriteCond %{HTTP_HOST} ^123\.456\.78\.9 
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

From a similar thread [webmasterworld.com] about redirecting 'dot-slashers', again the same results:

# Handle www.example.com/. variants 
RewriteRule ^\. http://www.example.com/ [R=301,L]
#
# Handle www.example.com./ variants
RewriteCond %{HTTP_HOST} ^www\.example\.com\.
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

I thought the lines might be conflicting with other Conds/Rules but my error_log is A-OK and the directory-level redirects do happen -- just not to the right places.

Any ideas? Is there an AWOL / or $ in the Cond statements? I'd love for people to be able to go to the intended page, just with the preferred domain name.

As always, thanks in advance for your expertise, and patience! :)

jdMorgan

11:26 pm on Feb 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Pfui,

Kind of rushing through today...

You may need to add


RewriteOptions inherit

to the .htaccess file, either in top-level directory, subdirectory, or both.

Also, it might be a RewriteBase problem -- check the server error log on those failed attempts.

It could also be an 'Alias' problem. If those subdirs are Aliased away from you in httpd.conf, then your top-level .htaccess won't run.

Jim

Pfui

12:44 am on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I appreciate your having even a quick look-see, Jim! Perhaps this additional info will help?
(crosses fingers)

1.) "RewriteOptions inherit" <-- Been there, done that (ages ago in all directories having an .htaccess to make sure unauthorized bots don't acccess any contents via external links).

2.) RewriteBase: None specified that I know of. Also, no RewriteMaps.

3.) Aliases: The httpd.conf directives look routine and have to do with icons (Alias /icons/ "/var/www/icons/") and cgi-bin (ScriptAlias /cgi-bin/ "/var/www/cgi-bin/"). On a related note -- I don't have any Sym Links controlling any directories, just the occasional CGI, etc.

4.) error_log (this domain's; Combined format): Clear. Nada.

FWIW, from my rewrite_log (ditto), re my attempting to visit...

http://www.example.com/directory/file.html

...here's the rewriting (line-broken to fit here):

my.computerIP.here - - [09/Feb/2006:15:39:23 -0800] [www.example.com/sid#8121dd4][rid#812dec4/initial] (1) 
[per-dir /path/to/primary/public_html/directory/] redirect to http://www.example.com/index.html [REDIRECT/301]

That's a perfect 301 -- if I'd wanted to go home:)

Man, I love-hate mod_rewrite.

jdMorgan

1:14 am on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's no reason for this to happen in the code that I see. Your rule redirects any URL-path requested from the IP address to the same URL-path in the specified domain.

I think there is some server misconfiguration or something that causes this. I've only ever seen a few reports, and basically, it makes no sense.

Jim

Pfui

1:44 am on Feb 10, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not wild about being the exception to the Rules... Regardless, thank you for your re-review, Jim.

Seeing as how most everything else mod_rewrite-related works, and I rely on it very, very heavily, I'm disinclined to awaken the tweaking giant that is my SysAdmin just in case something really important breaks in the process. Like, oh, perl. :)

And you never know, it may turn out to be totally unrelated to some major misconfig. Perhaps my .htaccess file size is getting too big again or someth-- wow. Wow!

(slaps head)

That just dawned on me! 140k seems to be my absolute maximum for all of its contents to work every time, all the time, and mine is 138k as I type. I'll start cutting chunks of my notes-to-self # comments and will post again if doing so does the trick!