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

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.

Related

how to 301 redirect from root domain to a page with htaccess

i am trying to auto 301 redirect from domain.com to domain.com/v2
ive tried this code
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^$ /v2/ [NC,L]
also tried
Redirect 301 / /v2/
i get the error 'site has tried to redirect you too many times'
is there any way to do this?
thanks for any help
Untested but this should redirect both domain.com and www.domain.com to the v2 subfolder:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
You need to replace example.com with your actual domain because of posting restrictions.
I suppose you have an endless redirect because you are not matching the end of the domain string with $. Redirection should only take place at the root domain.
you can use a write rule with ^$ representing the root of the site and rewrite it to the selected folder
RedirectMatch ^/$ /v2/
it redirects the root and only the root URL.
or also you can use
RewriteEngine On
RewriteRule ^$ /v2[L]
If you want external redirection(301) you can use R flag
RewriteRule ^$ /v2[L,R=301]
Another way to use it with RewriteEngine
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^$ v2 [L]
Original Answers
I hope I've helped

Redirect subdomain to folder infinitely redirects due to www rule

I'm trying to add a single subdomain to an existing website. We've added the appropriate 'A' record. We want staging.example.com to display the contents from example.com/test/ (and remain staging.example.com).
The .htaccess file (written by someone else) redirects all non-https to https, and all non-www to www:
RewriteEngine On
RewriteBase /
#Rewrite http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
#Rewrite non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
I tried to add a line like the following, just before the last RewriteRule:
RewriteCond %{HTTP_HOST} !^staging\.(.+)$ [NC]
However, it always gets in a redirect loop to www.staging.example.com ...
Suggestions much appreciated.
This is because you are directing BACK to HTTP with your WWW redirect. Change it to https:
EDIT
I would also invert your statments .. This is EXACTLY what I use -- And I can verify it works for me:
RewriteEngine On
# to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess redirect non-www to www except one directory not working

I want to redirect all non-www to www. Except for the requests pointing inside /cgi-bin/mail-dada/
I can't think of why the code below also redirects http://something.com/cgi-bin/mail-dada/mail.cgi/admin/ to the www-prepended version.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.something\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/mail-dada/ [NC]
RewriteRule (.*) http://www.something.com/$1 [R=301,L]
The rule in your question should be working.
I suspect a cache problem (maybe old rules ?).
Try it with another browser or clear your cache and try again.
Also, if you don't want to hard-code your domain name, you can do it that way (if you have only www or non-www subdomains)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/cgi-bin/mail-dada/ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Stop .htaccess redirecting everything to

I have setup my .htaccess to redirect requests from http ://subdomain.domain.com.tld, http ://www.subdomain.domain.com.tld and http ://domain.com.tld/folder (folder that contains contents of subdomain) to https ://subdomain.domain.com.tld so my single domain SSL certificate setup for subdomain.domain.com.tld is used at all times.
My problem is http ://domain.com.tld also redirects to https ://subdomain.domain.com.tld. How do I stop this?
RewriteOptions inherit
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^subdomain.domain\.com.tld
RewriteRule ^(.*)$ https://subdomain.domain.com.tld/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^folder$ "https\:\/\/subdomain\.domain\.com\.tld\/" [R=301,L]
Thank you.
These rules should replace everything after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com\.tld$ [NC]
RewriteRule ^(.*)$ https://subdomain.domain.com.tld$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com\.tld$ [NC]
RewriteRule ^folder/?(.*)$ https://subdomain.domain.com.tld$1 [R=301,L]
The first set of rules matches http://subdomain.domain.com.tld and http://www.subdomain.domain.com.tld
The second set matches http://domain.com.tld/folder, but does not match http://domain.com.tld without the "folder" part.
Edit: Corrected typo as noted in comments and escaped dots in RewriteConds.

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

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]