Is no-one of you using WWW redirection?

Events happening in the community are now at Drupal community events on www.drupal.org.
no2e's picture

I need help.
I want to redirect all my sites to the URL version without the www. prefix.

Please, could someone post his .htaccess?

I search since 22 days for a solution (here, here, here and here) - it hurts me, because I got time, I want to start playing with Drupal, but can't, until I know it will work :(

Comments

.htaccess

macieq's picture

First:
Where you have .htaccess file. (it should be in your domain base dir. Example:

if u have www.first-domain.com this domain is in your "root/drupal" direcotry of your multiple-domains tree.
foo.domain.com is in "root/drupal/sites/foo.domain.com"

the .htaccess file should be in the "root" folder

Second:
To redirect add folowing lines to .htaccess:

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

Where exemple is name of your domain in our case when u have www.foo.yourdomain.com.pl write:

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

I really....hmm

macieq's picture

Okey.. "it is" realy strange that u've searched for this 22 days... that is simple written in the .htaccess drupal default file. line 77-91

virtual

macieq's picture

if you have site in /drupal direcotory like VirtualDocumentRoot uncomment line 97 in .htaccess:

RewriteBase /drupal

it should help

Coincidentally, I ran into

Garrett Albright's picture

Coincidentally, I ran into this issue today for a multi-site project I'm working on, and found a way to fix it. I just came here to start a new thread to share it with the world, but you beat me to it. So here it is!

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

Apache's config syntax is truly the stuff of nightmares. That %1 is saying "used the matched pattern from the RewriteCond," whereas the $1 says "use the matched pattern from this same RewriteRule."

macieq: The problem with your suggestion is that it will require that RewriteCond/RewriteRule to be copy-and-pasted for each domain name the multi-site installation will use. In the case of my project, that's twelve domain names, with the possibility of more being added in the future by the clients (who shouldn't be touching .htaccess), so doing it manually for each domain name is less than ideal. Also, I'm not sure what RewriteBase has to do with this situation.

It works!

no2e's picture

Thanks Garrett Albright.

Now I use your two lines for www. to non-www. redirection - and it works for all my multisites.

But it works not for my default site, which is installed in a subdirectory. To get the default site redirection working too, I used the two lines which provides the default .htaccess, but I had to modify them a little bit:

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

(I never had the idea to trail the subdirectories only in the RewriteRule and not in the RewriteCond)

So, I inserted that four lines

RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.)$ http://example.com/subdirectory/drupal/$1 [L,R=301]

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

after the redirection block and before the RewriteBase block (line 90).
I did not set the RewriteBase (I left it commented). Actually those four lines are the only edit I made to the .htaccess file.

mhm

macieq's picture

Yes you have right. Im using what u've shown too. But i only show the method.
( i have 32 domains multi0-site project ;P)

I show you another Rule to do this:

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

This is method to add www.

Sorry for "base" but our friend who post this have a problem with links and it would help him

Greet!

.htaccess sheet

macieq's picture

Very usefull .htaccess chaet sheet

By the way, I submitted a

Garrett Albright's picture

By the way, I submitted a patch for this to the core Drupal project here:

http://drupal.org/node/352180

If you think Drupal should support this out of the box, go voice your support.

Does it work for multisites in subdirectories, too?

no2e's picture

As mentioned above, it works for me not for my default site, which is installed in a subdirectory.

Maybe it is not working for multisites, which lay in a subdirectory, too? (I did not test it yet, got only subdomain multisites atm)

If this is true, it would mean, that you would have to copy & edit 2 lines for each subdirectory multisite manually, correct? Or is there a general htaccess trick (like the one working for the other multisites), where you don't have to edit something manually?

Dealing with subdirectories

Garrett Albright's picture

Dealing with subdirectories is indeed a case which I didn't account for. But the "normal" rewrite code doesn't account for it either, so you'd have to do some custom .htaccess hacking either way.

htaccess is overwritten on update

Ivan_Dagreat's picture

Have used the htaccess file for 301s for years. My problem is every time I update Drupal (security fix) the htaccess file is overwritten and I have to backup/restore whatever setting I added.

I am looking for a module that could possible just handle the nowww/www redirect.

Adding this ability to the core as an option sounds like a great idea. This is the oldest seo issue out there.

What about using your C-Panel?

Lakeside's picture

As Garrett mentioned the .htaccess is the most common way to redirect.

You can also check your CPanel for redirecting domains.

You can look at Domain 301

IncrediblyKenzi's picture

You can look at Domain 301 Redirect module.

Likely the reason this won't appear in core is that it's not needed in the majority of cases. Either .htaccess changes or putting it in a virtualhost httpd.conf entry is the best practice.

Multisite

Group organizers

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

Hot content this week