"www" is added to browser address?

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
LVL 2
SandyCookeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caterham_wwwCommented:
> 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.
SandyCookeAuthor 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
ravenplCommented:
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.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

caterham_wwwCommented:
> 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]
karelrambousekCommented:
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-
ahoffmannCommented:
are you sure that this is not done by your browser?
SandyCookeAuthor 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

SandyCookeAuthor Commented:
PLEASE DISREGARD LAST COMMENT
SandyCookeAuthor 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?
SandyCookeAuthor 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
SandyCookeAuthor Commented:
favicon also doesn't appear for

http://domain.com/

(to avoid any trailing slash confusion)
ahoffmannCommented:
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"
caterham_wwwCommented:
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.
ahoffmannCommented:
> 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 :(
SandyCookeAuthor 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
caterham_wwwCommented:
Yes, change {2,4} into {2,5}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SandyCookeAuthor Commented:
Genius
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.