301 redirect based on request domain htaccess - .htaccess

so i'm looking to 301 redirect my old domain to my new one, although I would only like to do the redirect for a specific request domain.
currently I have
RewriteCond %{HTTP_HOST} ^www\.olddomain\.net[NC]
RewriteRule ^(.*)$ http://www.newdomain.net/$1 [R=301,L]
although it's not working. The redirect only needs to be done when it matches the domain the request is coming from with olddomain.net
Does anyone see anything wrong with that?
Thanks

You could remove the first line and just keep the second line nd it would still work.
RewriteCond %{HTTP_HOST} ^www\.olddomain\.net[NC] #remove this line
RewriteRule ^(.*)$ http://www.newdomain.net/$1 [R=301,L]
Removing that line will redirect everything (www and non-www links).
but the problem you are having is there is no space after .net and [NC]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.net [NC]
RewriteRule ^(.*)$ http://www.newdomain.net/$1 [R=301,L]

Related

301 redirect to new domain with same url structure

I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.
I've updated the .htaccess file on my old domain with:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.
I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.
Any idea on what could be wrong?
Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]
Better to clear your browser cache before testing this rule.
PS: You need to match hostname condition to old-host not the new-host.
This should redirect and retain the path after the main domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]

Redirecting multiple URL's to a subfolder using htaccess file

What I trying to achieve is redirect my domain with www and without it to a subfolder in my application.
Example:
I have www.mysite.in.I want to redirect the user to www.mysite.in/feature/index.php when he hits either www.mysite.in or only mysite.in,but I am not able to achive it.I either end up in a loop or the redirect does not work.
This is what I have tried
RewriteCond %{HTTP_HOST} ^mysite.in
RewriteRule ^(.*)$ http://www.mysite.in/feature/index.php$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.mysite\.in [NC]
RewriteRule ^(.*)$ http://www.mysite.in/feature/index.php$1 [R=301,L]
The above ends in loop.
Redirect 301 mysite.in http://www.mysite.in/feature/index.php
Redirect 301 http://www.mysite.in http://www.mysite.in/feature/index.php
In the above case first redirect works,second fails.
How can I resolve the issue.?
You don't need 2 rules, as they redirect both to the same URL.
There is a loop because you don't test the folder you're in. The RewriteCond on HTTP_HOST is useless because you always be on mysite.in or www.mysite.in (if not leave it).
Try this :
RewriteCond %{REQUEST_URI} !^/feature/ [NC]
RewriteRule ^(.*)$ http://www.mysite.in/feature/index.php$1 [R=301,L]

Redirect extension through htaccess

I want to redirect all "com" extensions to "org" extensions, even if they are https. This is what I have so far...
RewriteCond %{HTTP_HOST} domain.com$ [NC]
RewriteRule .* https://www.domain.org [R=301,L]
The problem is, this only redirects if it is "http". How can I also redirect if it is an https domain?
Any help would be appreciated. Thank You Much.
Try:
RewriteCond %{HTTP_HOST} domain.com$ [NC]
RewriteCond %{HTTPS}:s (on:(s)|off:s)
RewriteRule .* http%2://www.domain.org [R=301,L]
The second condition creates a capture group in the event that HTTPS is "on", otherwise, there is no capture group. The "s" is captured and then backreferenced using %2.
Additionally, you can redirect the URI as well by changing the rewrite rule line to:
RewriteRule ^(.*)$ http%2://www.domain.org/$1 [R=301,L]

How do I redirect all subdomain requests to primary domain in htaccess?

I need to redirect all subdomain requests to be redirected to my primary domain in my htaccess. I need it to also include some sort of wildcard redirect.
e.g. washington.mysite.com/ redirect to mysite.com/washington
but I need to to also redirect any old url on the subdomain to mysite.com/washington
e.g. washington.mysite.com/category.aspx?washington-attractions&CatID=17 redirect to my-site.com/washington
This is my code so far:
RewriteCond %{HTTP_HOST} ^washington\.mysite\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.washington\.mysite\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mysite\.com\/washington/$1" [R=301,L]
However it still appends the old URL to the new one
e.g. washington.mysite.com/category.aspx?washington-attractions&CatID=17 redirects to my-site.com/washington/category.aspx?washington-attractions&CatID=17
Basically I need the redirect washington.mysite.com/*(anything) to my-site.com/washington
Any suggestions would be much appreciated :)
RewriteCond %{HTTP_HOST} ^(www\.)?washington\. [NC]
RewriteRule ^(.*)$ http://www.mysite.com/washington [R=301,L]
In your RewriteRule you have /$1 which matches the (.*) wildcard set of parentheses. This is why you're getting the path from the old URLs appended.
I combined your 2 RewriteCondition's, making the (www\.) match optional with ?.
The [NC] flag is the nocase flag.
To clear the querystring, append ? to the end of the rewrite URL
RewriteCond %{HTTP_HOST} ^(www\.)?washington\. [NC]
RewriteRule ^(.*)$ http://www.mysite.com/washington? [R=301,L]

htaccess redirect not working as expected

I know I am missing something simple, but I can't get this redirect to work right. I have a parked domain that I want to redirect to the main domain. I am using:
RewriteCond %{HTTP_HOST} ^parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
The problem is that parked-domain.com/anypage.html redirects to main-domain.com and leaves off the rest of the URL. I need parked-domain.com/anypage.html to redirect to main-domain.com/anypage.html
EDIT
I am sure this is a cache thing with my browser but after I tried the first suggestion then changed it back, now I get this:
parked-domain.com/anypage.html redirects to www.main-domain.com/anypage.html
but
www.parked-domain.com/anypage.html does not redirect at all.
Part of the problem was with the cache, that got part of the redirect working. I ended up using this to get it all forwarding properly:
RewriteCond %{HTTP_HOST} ^parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.parked-domain.com$ [NC]
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
The first one should have worked for all cases, but it didn't. Adding the second redirect fixed it.
Try removing the ^ $ around your (.*)
RewriteRule (.*) http://www.main-domain.com/$1 [L,R=301]

Resources