How to redirect non-www to www URL's using htaccess? - apache

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.

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]

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]

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]

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]

Related

shared htaccess: redirect multiple domain roots

My situtation is that multiple domains share a htaccess file and I need to redirect the root / to each language folder.
So I want ONLY the root / of each domain to be redirected like:
www.my-en.com/ to redirect to /en/
and
www.my-de.de/ to redirect to /de/
Unfortunately I need to accomplish this with htaccess as I CANNOT change any other settings.
So far all solutions I found look like this
redirct 301 /old-path http://www.new-domain.com
which will always be applied to all domains using the htaccess.
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?my-en\.com$ [NC]
RewriteRule ^$ http://www.my-en.com/en/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?my-de\.de$ [NC]
RewriteRule ^$ http://www.my-de.de/de/ [R=301,L]
# Redirect others to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]

.htaccess redirect rules to www and htpps but to exclude subdomain

After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

htaccess redirect with sub-domain

I have a domain example.com and in .htaccess, I am forwarding non-www to www address using below code.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now I created a subdomain test.example.com and because of above rule, its going to http://www.test.example.com, so I thought to write it's own htaccess file to redirect www to non-www. So in the subdomain root, I have .htaccess file with below rule.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/$1 [R=301,L]
It's not working and going in a redirect loop, how do I fix it?
Take out the ! and the escapes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ http://test.example.com/$1 [R=301,L]
You don't need 2 rules. Just keep this rule in main .htaccess for your main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
Now RewriteCond %{HTTP_HOST} ^example\.com$ condition will only impact main domain leaving your sub domain untouched.
I found a very easy solution for this problem.
My main site document root was /home/example/public_html and subdomain document root was /home/example/public_html/test.
I just changed the document root of subdomain to /home/example/test and it worked.

htaccess 301 redirect www & non-www to non-www subfolder

I am trying to redirect www and non-www to non-www subfolder /forums.
I am currently using this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ http://example.com/forums/$1 [R=301,L]
It is working well, but there are some problems with this.
If I type the URL: example.com, it brings me to example.com/forums. -OK
If I type the URL: example.com/test, it brings me to example.com/test. -OK
If I type the URL: www.example.com, it brings me to example.com/forums. -OK
If I type the URL: www.example.com/test, it brings me to example.com/forums/test. -WRONG
How do I fix the occurring error for www.example.com/test?
I want www.example.com/test to lead to example.com/test.
Please help me, thanks!
Try following rule in your .htaccess file:
RewriteEngine on
Options +FollowSymlinks -MultiViews
# redirect empty URL to /forums
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^$ http://example.com/forums [R=301,L]
# non forums handler
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/forums [NC]
RewriteRule ^(.+)$ http://example.com/$1 [R=301,L]
If you don't have any other subdomains pointing at the same directory (or it doesn't matter that they are redirected too), then you might simply try this:
RewriteEngine On
RewriteRule ^$ /forums/ [R=301,L]
This redirects any requests to www.example.com/ and example.com/ to the subfolder /forums/ and leaves all other requests untouched.
Assuming that your given test cases are the only ones to be considered, then this should work. If you have any additional cases then the rule(s) might need to be extended.