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

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

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]

Redirect subdomain to main domain in Joomla

I need to redirect all subdomains of my website to the main domain starting with www:
I have tried to add this code to my .htaccess:
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
but I have a redirect loop error.
What do you suggest?
Thanks
Your rule will indeed cause redirect loop. Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
If you want demo.domain.com to be redirected to www.domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NE]
I'm using this code in my .htaccess file at the root directory of the domain or subdomain
Redirect all requests from subdomain:
Redirect / http://www.example.com/
Redirect all requests from folder sub on the domain to the subdomain:
Redirect /sub http://sub.example.com/

htaccess rewrite condtion only for specific domain not sub folder

My current code is this
Redirect 301 /index.html http://example.com/
Redirect 301 /about.html http://example.com/
Redirect 301 /contact.html http://example.com/contact.php
However this is affecting another domain that points to a sub folder of this site.
is there a way to say something like if(DOMAIN != 'example2.com'){...}
i also use other RewriteRule functions like this
RewriteRule ^events/?$ events_page.php [L]
and also this to remove www
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
Can't think of a particularly elegant way, but the following should do the job:
RewriteCond %{HTTP_HOST} !^example2\.com
RewriteRule /(index|about)\.hmtl http://example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} !^example2\.com
RewriteRule /contact\.hmtl http://example.com/contact.php [R=301,L]

.htaccess redirect request from specific domain to another domain

I have an apache server hosting a site. I have 2 domains pointing to this server, www.mysite.se and www.mysite.com.
I'm trying to figure out how to in the htaccess file to redirect traffic coming from the www.mysite.se domain to www.mysite.com/se/
I've tried several ways but cannot get it to work. I always just end up on the root of the site, as in www.mysite.com instead of the /se/ path.
This is what I've tried so far:
RewriteEngine On
RewriteCond %{http_host} ^www\.mysite\.se [NC]
RewriteRule ^(.*)$ http://www.mysite.com/se/ [R=301,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$ [NC]
RewriteRule ^(.*)$ /se/$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule ^(/)?$ se [L]
What am I doing wrong?
This seems to have finally done it!
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?mysite.se$
RewriteRule (.*)$ http://www.mysite.com/se/ [R=301,L]

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.