.htaccess 301 Redirect: www2 to www - .htaccess

I want to redirect just one URL:
www2.mydomain.com to www.mydomain.com
I don't want to redirect sub directories, just that top url, if I find another one then I could write another redirect, but I can't seem to get this working?

Try adding this in the htaccess file in your www2 document root:
RedirectMatch 301 ^/$ http://www.example.com/
If the document root is the same for both www and www2 then you need to check the HOST header and use mod_rewrite to do that:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www2\.example\.com$ [NC]
RewriteRule ^$ http://www.example.com/ [L,R=301]

Related

Redirect only root directory to another site

I'm on a subdomain and only want to redirect from this subdomain shop.example.com to example.com, but I want all other urls from shop.example.com still to work and be kept. Like shop.example.com/checkout should not be affected by this rule only /
In the subdomain directory shop.example.com I tried this in .htaccess
Redirect 301 / https://example.com
But then all is redirected
Try with below rule, I am using mod_rewrite.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^shop\. [NC]
RewriteRule ^$ http://example.com [R=301,L]

.htaccess redirect only root of a subfolder to another subfolder?

I have a site in "example.com/a" and would like to redirect 301 just url "example.com/a" to "example.com/b" and other data in "example.com/a" dont change address.
create .htaccess in "example.com/a" and try using several code like:
RewriteEngine on
RewriteRule ^$ http://www.example.com/b [R=301,L]
and
RedirectMatch 301 ^/$ https://example.com/b
and its redirect root but all other data in "example.com/a" show 404 error.
I have a lot of photos in "root/a" , just want to redirect the "root/a" address to "root/b" and the address of the photos remains the same.
For example, the "root/ a/pic.jpg" address will remain unchanged before and after the redirect,
How can i just redirect the "example.com/a" and other old address not change?
If I understood you well you need to redirect root/a to root/b while maintaining location of the rest of the pages, files?
This will do:
# Redirect Root Only
<IfModule mod_rewrite.c>
RewriteEngine On
#Uncheck line below to redirect 'index.php' as well
#Redirect 301 /a/index.php http://example.com/b/
#Redirect root only
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/a/$
Rewriterule ^(.*)$ http://example.com/b/ [L,R=301]
</IfModule>
# END Redirect Root Only
Need to redirect index.php, index.html, or other index pages too? Uncheck the line in code above. Because %{REQUEST_URI} ^/a/$ excludes all direct file paths including /a/index.html,index.php,index.py,etc.

Redirect 301 specific pages and the rest to just one page

First I need to redirect these pages to another page in a different domain
Redirect 301 /example1 http://newdomain.com/test1
Redirect 301 /example2 http://newdomain.com/random1
Note the pages are not the same in the new domain (e.g., /example1 to /test1)
After that, I need redirect the rest of the pages to newdomain.com
E.g., Redirect 301 (everything else) to http://newdomain.com
Try below rule, using mod rewrite I am assuming you have mod rewrite enabled.
RewriteEngine On
RewriteRule ^example1$ http://newdomain.com/test1 [R=301,L]
RewriteRule ^example2$ http://newdomain.com/random1 [R=301,L]
RewriteCond %{REQUEST_URI} !^(example1|example2)
RewriteRule ^ http://newdomain.com [R=301,L]
If you want to use mod-alias , you can use these redirects :
RedirectMatch 301 ^/example1/?$ http://example.com/test1
RedirectMatch 301 ^/example2/?$ http://example.com/random1
#redirect everything else to the homepage of example.com
RedirectMatch ^.+$ http://example.com/
Clear your browser cache before testing these redirects.
Try this in your .htaccess
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

Old Domain 301 Redirect to New Domain with HTAccess Wildcard

I have a old domain lets call it
example.com
I want to do a 301 redirect of all its page and its homepage to
newsite.com
So for example
example.com/category/page.html
Should 301 redirect to
newsite.com/category/page.html
And also
example.com should redirect to newsite.com
Be it with www or without www
I tried the following
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://www.newsite.tv/$1 [L,R=301,NC]
My folder structure is
index.html
.htaccess
When I go in the site, be it with wildcard or not, it only load the index.html and the .htaccess redirect is not working.
I did enable modrewrite
Can anyone guide me to set up the right .htaccess for this case.
Thanks!
You can also use RedirectMatch directive .
Try this in Olddomain/.htaccess :
RedirectMatch ^/(.*)$ http://newsite.com/$1

301 Redirect Conflict with www redirect

I am having an issue with my 301 redirects.
I just upgraded my site from an html based site to a joomla site. SO I am trying to redirect the 50 or so pages to the new joomla based navigation.
SO what is working:
rewrite rule to remove index.php, and the www., and 301 redirects
What isn't working:
301 redirect with a www in front of it.
www.sample.com/page.html
It sends them to the home page instead of the page, it takes them to the home page.
Here is my www rewrite rule.
## Redirects to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} www.sample.com
RewriteRule (.*) http://sample.com/$1 [R=301,L]
Here is my 301 rule
Redirect 301 /page.html /page
Thanks for the help.
If you are trying to remove the www then you might want to change the matching up some. Try your rule like this.
## Redirects to www.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.sample\.com$ [NC]
RewriteRule (.*) http://sample.com/$1 [R=301,L]
RewriteRule ^page.html$ /page [R=301,L]
RewriteRule ^page2.html$ /page2 [R=301,L]

Resources