15

I have a website say http://www.example.com/ in the root of my website, I have added .htaccess file to redirect any request of http://example.com/ to http://www.example.com/

Recently I have created a new section "Videos" so the URL for videos is http://www.example.com/videos/ . In this video folder I have another htaccess file which is performing rewriting for video entries. When I am trying to access http://example.com/videos/ then its not redirecting me to http://www.example.com/videos/

I think .htacces is not inheriting the previous rules from the parent directory. Can anyone please tell me what can be the rule I can add in the .htaccess file of /videos/ folder so that any request for http://example.com/videos/ will be redirected to http://www.example.com/videos/ URL.

djmzfKnm
  • 26,679
  • 70
  • 166
  • 227

4 Answers4

48

This is a more generic solution, because it can be used with any domain name without having to specify the specific domain name in each .htaccess:

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

The contrary is also possible (www to non-www):

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
  • 2
    I'm in Magento and this code redirects //mywebsite.com/some/url to //www.mywebsite.com/index.php. Is there an additional step here to keep the URL persisting? – Thomas Bennett Feb 06 '14 at 19:56
  • 1
    I think you must remove one "/" from the rule near $1: RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L] – Bahadir Tasdemir Feb 02 '16 at 09:56
9

I think it may just be that your existing rule is too strict, and is not getting triggered in your subdirectory because of this. Something like this should work site-wide:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [R=301]
Laurence Gonsalves
  • 137,896
  • 35
  • 246
  • 299
1

This is Best Solutions to redirect non-www to www URL's using htaccess. using this code in htaccess file and check url.

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^mydomain.com [nc]
rewriterule ^(.*)$ http://www.mydomain.com/$1 [r=301,nc]
Baby Groot
  • 4,637
  • 39
  • 52
  • 71
0

I recommend everyone to stick with the below method it will only redirect the main domain only not any other sub-directory or sub-domain in your host.

# Redirect non-www to www only for main domain 
# recommended if you have any/some sub-domain(s)
RewriteEngine on
RewriteBase /

# Replace yoursite and .tld respectively
RewriteCond %{HTTP_HOST} ^yoursite\.tld$
# Replace yoursite.com
RewriteRule ^(.*) http://www.yoursite.com/$1 [R=301]

Use this method if you are really sure and I hope you will that you won't ever use sub-domain with your website i.e. subdomain.yoursite.com or whatever. Than you're welcome to use the below method. "Saying Again make sure if you really want to use this method"

# Redirect all non-www to www including subdomain(s)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

The below method will redirect all www url to non-www including your sub-domains although you cannot use www.subdirecty.yoursite.com it will prompt you an error with 500.

# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
shaz3e
  • 316
  • 2
  • 14