We help IT Professionals succeed at work.

"www" is added to browser address?

SandyCooke
SandyCooke asked
on
Medium Priority
422 Views
Last Modified: 2010-03-04
When I request

http://domain.com/apple/

"www" is added to the browser address to make

http://www.domain.com/apple/

Can I stop this happening using .htaccess?

Sandy
Comment
Watch Question

Top Expert 2008

Commented:
> Can I stop this happening using .htaccess?

I think this happens before the .htaccess file is reached, because it would only happen in a context of a .htaccess file, if you did define a rule to redirect all "non-www requests" to "www requests". If you don't have such a redirection in your .htaccess file, the redirect comes from an earlier stage of processing.

Author

Commented:
I have done a bit more playing around and it seems that the "www" is only added if a trailing slash is omitted from the page request:

http://domain.com/banana

becomes

http://www.domain.com/banana/

whereas

http://domain.com/banana/

stays as

http://domain.com/banana/

Does this mean that I can use .htaccess to add a trailing slash to all requests missing a slash and I will never encounter this problem again?

How would I do this?

Sandy
Top Expert 2005

Commented:
Please check http://httpd.apache.org/docs/2.0/mod/core.html#usecanonicalname
When You will not add ending /, apache sends redirect to itself with the ending slash.
Top Expert 2008

Commented:
> Does this mean that I can use .htaccess to add a trailing slash to all requests missing a slash

yes, that would be possible if you don't have access to the https.conf.

this method doesn't use the -d command to check for existing dirs, which might save performance

RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ - [C]
RewriteRule ^(.+) /$1/ [R=301,L]
Hello,
if using virtual hosts, use only domain.com for it,
if not use ServerName domain.com in global are.
This should work fine.
-K-
are you sure that this is not done by your browser?

Author

Commented:
I have added

RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ - [C]
RewriteRule ^(.+) /$1/ [R=301,L]

to

/home2/myusername/public_html/.htaccess

I have not touched any other .htaccess files

When I make a request for

http://golo.co.uk/work

Opera 8.0.1

Author

Commented:
PLEASE DISREGARD LAST COMMENT

Author

Commented:
@ ahoffman

If the browser is adding the "www" then why does a request for

http://domain.com

become

http://domain.com/

without adding "www"

?

Also a request for

http://domain.com/banana/

stays as

http://domain.com/banana/

with no added "www"

?

-----------------------------------------

@ ravenpl

If Apache automatically adds a trailing slash then why does a request for

http://domain.com/banana

become

http://www.domain.com/banana/

whereas

http://domain.com/banana/

stays as

http://domain.com/banana/

?

Unless Apache has also been configured by my host to add a trailing slash AND "www" to every request without a trailing slash?

-----------------------------------------

@ karelrambousek

What do you mean by

if not use ServerName domain.com in global are.

?

p.s. I am on a shared reseller hosting plan if relevant

---------------------------------------------------

@ caterham_www

I have added

RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ - [C]
RewriteRule ^(.+) /$1/ [R=301,L]

to

/home2/myusername/public_html/.htaccess

I have not touched any other .htaccess files

When I make a request for

http://mydomain.com/banana

Safari 2.0.3
Opera 8.0.2
Firefox 1.0.6
Camino 0.8.4

all still produce

http://www.mydomain.com/banana/

whereas

http://domain.com/banana/

stays as

http://domain.com/banana/

same as before...

also FYI a request for

http://domain.com

produces

http://domain.com/

with no added "www"

?

Is it possible that Apache has been configured by my host to add "www" to every request without a trailing slash EXCEPT if the the request is for the root directory?

Author

Commented:
Also (and somewhat bizarrely) I have a favicon in each directory:

http://domain.com/favicon.ico
http://domain.com/banana/favicon.ico

linked with

HREF="favicon.ico"

in

http://domain.com/index.php
http://domain.com/banana/index.php

IT APPEARS FOR

http://www.domain.com/
http://domain.com/banana/
http://www.domain.com/banana/

BUT NOT FOR

http://domain.com

?

Could this be a clue?

Sandy

Author

Commented:
favicon also doesn't appear for

http://domain.com/

(to avoid any trailing slash confusion)
still no answer if your browser does the completion
> Opera 8.0.1
please go to  Tools->Preferences _|Advanced|_ Network and disable "Server name completion"
Top Expert 2008

Commented:
Try to prefix the last rule with the HTTP_HOST

RewriteEngine On
RewriteCond %{REQUEST_URI} !/$
RewriteRule !\.[a-z]{2,4}$ - [C]
RewriteRule ^(.+) http://%{HTTP_HOST}/$1/ [R=301,L]

But if there is an other trailing slash fix in httpd.conf in place, that won't work, of course. If the current 301 redirect comes from mod_dir, you should be able to catch it.

favicon is a browser issue.
> favicon is a browser issue.
true from technical view :-)
but from user's view the server has to provide the proper file (name) and format in the right place.

> Also (and somewhat bizarrely) I have a favicon in each directory:
probably not the way favicons are designed for :(

Author

Commented:
@  caterham_www

you are obviously some kind of dude, it works perfectly

BUT

there is a resulting issue which I hope you can resolve, namely

when I get a 404 I now get

http://domain.com/404.shtml/

(the 404.shtml was never visible before)

I assume this is because a trailing slash is being added

BUT

a request for

http://domain.com/index.php
http://domain.com/banana/index.php

stays as

http://domain.com/index.php
http://domain.com/banana/index.php
 
with no added slash

I'm not sure how the 404, 403 errors are generated - strange that they get a slash while a request for a php file doesn't?

Can we keep the 404 "invisible"?

Sandy
Top Expert 2008
Commented:
Yes, change {2,4} into {2,5}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Genius
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.