I just installed Drupal 5.0 RC1 and wanted all request with the www. prefix in the URL to redirect to their equivalent without the www.

Example

What is happening is in the second example and any request with the www. prefix redirects the user back to the root domain, or front page of the site. When testing out the Drupal.org site it works like I want, but my site isn't.

My .htaccess file

...

# If you want the site to be accessed only WITHOUT the www. prefix, adapt
# and uncomment the following:
RewriteCond %{HTTP_HOST} ^www\.ferraiuolo\.name$ [NC]
RewriteRule .* http://ferraiuolo.name/ [L,R=301]

...

You can test this at:

If anyone has some insight on this it would be much appreciated, I tried searching around this site for a while and couldn't find anything that seem similar to this situation and no other solutions I tried seem to do anything. Thanks!

Comments

belio’s picture

I didn't test this, but try

RewriteCond %{HTTP_HOST} ^www.ferraiuolo.name
RewriteRule ^(.*)$ http://ferraiuolo.name/$1 [R=301,L]

for the last lines above.

eferraiuolo’s picture

Thank you this worked!

I'm surprised the way I had it didn't work... .htaccess files are foreign to me and I was just going by what was commented in the default file. Maybe the default .htaccess file for Drupal should be updated to fix this type of issue.

Technically speaking what was different between what I had and what you posted?

Thanks again!

ff1’s picture

The '$1' at the end of the RewriteRule is used to add the rest of the path to the domain name.

Glad you got it working :)

ff1’s picture

RewriteCond %{HTTP_HOST} ^www\.fantasyformula1\.uni\.cc$ [NC]
RewriteRule ^(.*)$ http://fantasyformula1.uni.cc/$1 [R=301,L]

I copied this straight from http://no-www.org/

Hope that helps :)
ian
http://fantasyformula1.uni.cc

eferraiuolo’s picture

Think these changes should be submitted as updates to the core .htaccess? Assuming that this is the desired functionality over just have any request with the www. prefix redirecting back to the front page of the site only not matter what came after the top-level domain extension.

ff1’s picture

dtabach’s picture

Is there any functionality difference between the code provided by belio:

RewriteCond %{HTTP_HOST} ^www.ferraiuolo.name
RewriteRule ^(.*)$ http://ferraiuolo.name/$1 [R=301,L]

and the one by ff1:

RewriteCond %{HTTP_HOST} ^www\.fantasyformula1\.uni\.cc$ [NC]
RewriteRule ^(.*)$ http://fantasyformula1.uni.cc/$1 [R=301,L]

?

The fact is that first one worked for me (drupal 4.7.4), and the second one did not...

Durval Tabach

ff1’s picture

Here are the differences between the two pieces of code.

The \ (backslash) character is used in the second RewriteCond to escape the . (period) character which would otherwise have special meaning. However, the special meaning is simply 'Matches any one character', so it should still work without the backslash.

The [NC] directive is used at the end of the second RewriteCond to indicate that the host name is not case sensitive. eg. it will work equally well with WWW.EXAMPLE.COM as it would with www.example.com. Most webservers assume this anyway, but it is still a good idea to include it.

The $ (dollar) character is used in the second RewriteCond to indicate the end of the host string. However, this is only required since the [NC] directive is also being used after the host string.

So with only these differences, I really don't know why both of these did not work. Can you post the exact lines used from your own .htaccess file? Is it possible it could have just been a typo?

See http://etext.lib.virginia.edu/services/helpsheets/unix/regex.html for more details about regular expressions if you are interested.

Hope that helps.
ian

dtabach’s picture

I have just made another test to make sure there was no typo.

This code DOES work:

RewriteCond %{HTTP_HOST} ^www.dtabach.com.br
RewriteRule ^(.*)$ http://dtabach.com.br/$1 [R=301,L]

And this code does NOT work:

RewriteCond %{HTTP_HOST} ^www\.dtabach\.com\.br$ [NC]
RewriteRule ^(.*)$ http://dtabach.com.br/$1 [R=301,L]

Using the second code, the home pages are fine, but I get a 'page not found' message (the browser's default one, not the one provided by drupal) when I try to visit either www.dtabach.com.br/anything or dtabach.com.br/anything.

Durval Tabach

dtabach’s picture

I was editing a local copy of the .htacces file then uploading it to server using DreamWeaver (Mac OS X).

When I used Cpanel to directly edit the remote .htacces file, both codes worked.

Probably the process of saving/uploading the file with DreamWeaver changes its character encoding from UTF-8 to anything else.

Durval Tabach

rajenthakur@yahoo.com’s picture

Hello,
I am trying to use your code witch is working well.
Thanks,
I have one more problem, i want to change http://abc.com/abc.php, how to change abc to http://abc.com/xyz/ but back end open abc.php .

please solve this problem.

Thanks and Regards

Rajendra