Subdomain url redirect to a URL - .htaccess

We have about 50 URLs that need to be redirected, but subdomain names may differ, for example:
http://abc.OLDdomain.com > http://cba.NEWdomain.com
http://foo.OLDdomain.com > http://bar.NEWdomainc.com
Most of the answers suggest rules, but in our case source / target subdomains often differ, so it's not a straightforward redirect rule that could solve it.
Also, can you do this type of redirect without rewrite rules?
So ideally something like
redirect 301 abc.OLDdomain.com http://cba.NEWdomain.com
Which does not currently work
I'm asking if there's a way to do it without rewrite rules because the htaccess is managed by someone who's not tech savvy but understands simple redirect directives such as the one above.
My current working solution with rewriterule/rewritecond is:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^abc.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://cba.NEWdomain.com%{REQUEST_URI} [R=302,NC,L,QSA]
RewriteCond %{HTTP_HOST} ^foo.OLDdomain.com$ [NC]
RewriteRule ^(.*)$ http://bar.NEWdomainc.com%{REQUEST_URI} [R=302,NC,L,QSA]
If there's a one liner alternative without "RewriteCond and RewriteRule" that would be great

Related

301 Redirect from http to https and new page name

I have a site that is going from a non secure HTML/PHP site to a secure Wordpress site. All pages are changing. Also going from www to non-www.
So as an example
http://www.sitename.com/contact.php
will become
https://sitename.com/contact-us/
I know how to do a typical redirect in .htaccess:
Redirect 301 /oldpage.html http://www.example.com/newpage.html
But I'm not sure how to do this when both the page location and the HTTP/HTTPS is changing as well as the www going away. The site ranks pretty well and I don't want to lose any of that.
I've researched but can't find an example of doing both at the same time.
You should be able to do using rule like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^contact\.php$ https://%1/contact-us/ [R=301,L,NC]
Since your page names are different you will need multiple rules like this.
Make sure to keep these rules above all other rules.
You can use the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\. [OR]
RewriteCond %{HTTPS} off
RewriteRule ^contact\.php$ https://example.com/contact-us [NC,L,R=301,NE]
The rule above rewrites the following requests :
http://www.example.com/contact.php
or
http://example.com/contact.php
or
https://www.example.com/contact.php
to
https://example.com/contact-us

htaccess apply rule to all but two domains

here's the current situation
we have multiple domains on same server (brand protection) pointing at the same content.
All have been redirected to one default domain with 301 redirection.
However, i got a task to exclude one domain from the rule (with or without www).
Rewritecond %{HTTP_HOST} !^www\.basicdomain\.com
RewriteRule ^(.*)$ http://www.basicdomain.com/$1 [R=301,L]
So after the change it should say
If user types basicDomain2.com or www.basicDomain2.com do nothing.
If user types any other doman name with or without www, make redirection to www.basicdomain.com
Can anyone help me with this?
Have you tried this one:
Rewritecond %{HTTP_HOST} !^(www\.)?basicdomain\.com
RewriteRule ^(.*)$ http://www.basicdomain.com/$1 [R=301,L]
The ()? arround www. should make it optional.

Need .htaccess assistance

We have an existing site, let's call it ourdomain.com. We moved content over from a site that is shutting down, let's call it legacydomain.com. We pointed the legacy domain at our server to the /public_html/ folder.
What I need is an .htaccess that will redirect legacydomain.com or legacydomain/anything-here to ourdomain.com/legacydomain/ with nothing else appended to the URL.
However, I also need a few specific URLs to redirect to certain destinations, and they don't really follow a pattern. For example:
legacydomain.com/something.html to ourdomain.com/legacydomain/something.html
legacydomain.com/another.html to ourdomain.com/legacydomain/folder/another.html
This is what I have tried:
RewriteCond %{HTTP_HOST} ^www\.legacydomain\.com$ [NC]
RewriteRule (.*) http://www.ourdomain.com/legacydomain/$1 [R=301,L]
Redirect 301 /something.html http://www.ourdomain.com/legacydomain/another.html
It mostly works, but if I visit legacydomain.com/anything-here it doesn't even attempt to rewrite, it just keeps the domain the same and gives a 404. And also I have a feeling that even if it did work, something like legacydomain.com/anything-here/more-stuff would get rewritten as ourdomain.com/legacydomain/anything-here/more-stuff which I don't want.
Only other thing in the .htaccess is rewriting non-www to www, and the standard WordPress stuff. Any guidance would be greatly appreciated. Everything above should have an http:// and www in front for the examples, but it wouldn't let me post that many "links".
For each specific rewrite you would need two lines, as follows. Depending on your existing config you may need to add a slash at the beginning of the RewriteRule in front of something.html if this doesn't work.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule something.html http://ourdomain.com/legacydomain/something.html [R=301,L]
Then you would use a catch-all for everything else.
RewriteCond %{HTTP_HOST} legacydomain.com
RewriteRule (.*) http://ourdomain.com/legacydomain/ [R=301,L]
Personally, I would go for the simplest solution which doesn't use mod_rewrite. First, just redirect the specific pages to wherever they need to go.
Redirect 301 /something.html http://ourdomain.com/legacydomain/something.html
Redirect 301 /another.html http://ourdomain.com/legacydomain/another.html
Then, simply redirect everything else to the base URL.
RedirectMatch 301 (.*) http://ourdomain.com/legacydomain/
These must be put in your .htaccess file before the RewriteEngine on statement.

htaccess redirects from URLs on one domain to the root of another domain

Currently what is happening is people are accessing old URLs from google like icpaweb.com/site/pages/about-us/ and being sent to their corresponding urls on icpaweb.org : icpaweb.org/site/pages/about-us.
What I want is to send people from: icpaweb.com/site/pages/about-us to icpaweb.org/ without any of the succeeding url segments.
How do I do this?
If you have to use an .htaccess file, you can use mod_rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} icpaweb.com$ [NC]
RewriteRule .* http://icpaweb.org/ [R=301,L]
That will 301 redirect all requests for icpaweb.com to the index root of icpaweb.org. If you don't want 301, it can just be R.
You'll need to replace or turn off whatever mechanism is doing your redirecting now, they may not be compatible.
Use an url rewrite rule.
2 steps:
Write a RewriteCond so that the following rewrite rule only apply for url with host being icpaweb.com like RewriteCond %{HTTP_HOST} icpaweb.com$ [NC] The [NC] is for case insensitive match
Write a rewrite rule that convert all input to what you want like RewriteRule ^.*$ http://icpaweb.org/ [L]The [L] is to stop the rewriting to this rule if rule executed.

htaccess redirect for subdomains -> similarly-named subdirectories?

I'm restructuring a web site with a great deal of content currently parked at URLs that look like this.
http://string.domain.com/year/month/dd/string-pulled-from-title
For various reasons, I'd like to park all new content at URLs that looks like this
http://www.domain.com/blogs/string/year/month/dd/string-pulled-from-title
I'd like to make the change for future content, but don't want all the old stuff to go 404.
I believe a 301 redirect rule in my htaccess will do the trick, sending all referred traffic coming in through old links to the new formats.
But what should this rule look like? I've read a few tutorials but haven't found this exact case in any examples.
Note, I don't want to do this for all subdomains, only for about 10 specific ones. So if someone could help me figure out one of these, then I can copy paste it 10 times in my htaccess for each subdomain and be set.
Drop this into the .htaccess file of the old site (adjusting the domain to your actual one):
RewriteEngine On
RewriteRule ^(.*)$ http://example.com/blogs/string/$1 [R=301]
This will grab this part of the URL at the old site:
year/month/dd/string-pulled-from-title
and redirect it to the new site under the new location:
blogs/string/year/month/dd/string-pulled-from-title
Alternatively, if you want something a little more variable like, without having to custom fix each .htaccess, drop this in the file for each subdomain instead:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
If you're redirecting to the same domain, and it includes the www, adjust the rewrite rules to the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*).example.com
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/blogs/%1/$1 [R=301,L]
Note the second RewriteCond which checks to make sure that the URL requested does not include the leading www, which may lead to an endless redirect if the destination URL itself includes www and would try and redirect that subdomain as well.
%1 grabs the first capture group from the line above.
$1 references the first capture group on the same line.

Resources