How to redirect using .htaccess file - .htaccess

I'm trying to do something like this...
Redirect www.mysite.com/directory/* to my subdommain.mysite.com/directory/*
Please help me
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^directory/(.*)$ http://subdommain.mysite.com/directory/$1 [R=301,L]
I want like it

Try adding this to your .htaccess file if you want to redirect all requests from www.mysite.com to subdomain.mysite.com:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)$ http://subdomain.mysite.com/$1 [R=301,L]
Try adding this to your .htaccess file if you want to redirect only requests for /anydirectory/ from www.mysite.com/anydirectory/ to subdomain.mysite.com/anyotherdirectory/:
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^anydirectory/(.*)$ http://subdommain.mysite.com/anyotherdirectory/$1 [R=301,L]

Related

.htaccess 301 Redirect Except Main Page for .html

I'm trying to redirect all URLs in www.oldexample.com/index.html to www.newexample.com/index.html except the homepage (www.oldexample.com/index.html).
I have tried this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.oldexample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [R=301,L]
but it didn't work. It is redirecting all pages.
How can I make it work?
Please try with below,
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule ^(.*)$ http://www.newexample.com/$1 [R=301,L]

.htaccess RewriteRule change domain .com to .jp

I want to redirect requests on abc.com to abc.jp by adding this line to .htaccess file
RewriteRule ^(.*)abc\.com(.*)$ $1abc.jp$2
But it didn't work. So confusing...
Try using RewriteCond to match the domain, then redirect using RewriteRule
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?abc.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^(.*) http://abc.jp$1 [NC,L,R=301]

Redirect with htaccess if url of multiple subdomain not contains directory

And I have a lot of subdomain:
sub1.abc.com, sub2.abc.com,.., subn.abc.com with the same directory: true-dir-1, true-dir-2, true-dir-3
I want to
If url not contains (true-dir-1, true-dir-2, true-dir-3) then redirect to subdomain
For example:
sub1.abc.com/false-dir redirect to sub1.abc.com
sub2.abc.com/false-dir redirect to sub2.abc.com
Pls help me use .htaccess to redirect it..
Thanks so much!
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub1\.abc\.com$ [NC]
RewriteRule !^true-dir-1 / [L,NC,R=302]
RewriteCond %{HTTP_HOST} ^sub2\.abc\.com$ [NC]
RewriteRule !^true-dir-2 / [L,NC,R=302]
RewriteCond %{HTTP_HOST} ^sub3\.abc\.com$ [NC]
RewriteRule !^true-dir-3 / [L,NC,R=302]
You can use that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.abc\.com$ [NC]
RewriteRule !^%1 / [L,NC,R=302]
Change [R=302] for [R=301] when test work well.

Redirecting a web folder directory to another htaccess

Sorry this has no doubt been asked multiple times before, I just want clarification that the following code will redirect any url on olddomain.com to the newdomain.com homepage not the equivalent url:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^olddomain\.com$
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
Also if I wanted any subdomain on olddomain.com eg.subdomain.olddomain.com to go to the homepage of newdomain.com what would I have to do? Can I use a universal selector or would I have to write a condition for each subdomain like so:
RewriteCond %{HTTP_HOST} ^subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.subdomain.olddomain.com$
RewriteRule ^(.*)$ http://subdomain.newdomain.com/$1 [R=301,L]
Both of attempts are not correct as first will redirect:
http://olddomain.com/foobar to http://newdomain.com/foobar
not to the homepage of newdomain. Same is the problem with 2nd rule.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://www.newdomain.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.olddomain\.com$ [NC]
RewriteRule ^ http://subdomain.newdomain.com/ [R=301,L]

How to change .htaccess to redirect all non-www links to www pages?

How can I redirect all non-www links to www links? I have found solutions on the internet, but they only redirect the domain name.
How do I make this general:
http://example.com/testing should redirect to http://www.example.com/testing?
try something like this
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.yourdomain.com/$1
If you want something generic that works for any domain, you can try something like:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+)$
RewriteRule ^(.*)$ http://www.%1/$1

Resources