• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

"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
0
SandyCooke
Asked:
SandyCooke
  • 8
  • 4
  • 3
  • +2
1 Solution
 
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.
0
 
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
0
 
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.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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

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

http://domain.com/

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 4
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now