views:

57

answers:

2

Hey guys! Can you please advise me how to modify my .htaccess so that

http://example.com (non-www without trailing slash)
http://example.com/ (non-www with trailing slash)
http://www.example.com (www without trailing slash)

will be permanently redirected (301) to

http://www.example.com/ (www with trailing slash)?

Additionally, are there general rules to apply this "behavior" to subfolders

http://example.com/subfolder
http://example.com/subfolder/
http://www.example.com/subfolder
=> http://www.example.com/subfolder/

and subdomains (reversely here)

http://www.subdomain.example.com
http://www.subdomain.example.com/
http://subdomain.example.com
=> http://subdomain.example.com/

as well? As I'm completely new to this, please be kind... =)

Thanks! Nel

A: 

I think this will do the trick:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301] 
Eric Petroelje
+1  A: 

These three rules should do all the things you want:

RewriteEngine On

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

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

# Add trailing slash to all URIs without one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301] 
You
Hey You! =) I think that is what I'm looking for - thanks a bunch! Will I have to add "RewriteEngine On" and "RewriteBase /" here as Eric quoted?
Nelly
Only RewriteEngine On, i believe. (I'll edit my answer as well)
You
Thanks again, I'll try this asap.
Nelly