Redirect extension through htaccess - .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]

Related

Redirect after a RedirectRule has taken place to other website

I have a website, let's say www.example.com but this used to be www.example.nl. All traffic from www.example.nl is now redirected to www.example.com, but as we changed some naming conventions, www.example.nl/seeds now has to redirect to www.example.com/nl/flower-seeds.
The .htaccess file I got contains the following code:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
When I navigate to www.example.nl/seeds/ I end up at www.example.com/seeds/ which ends up in a 404 because I'm missing the /nl/flower- part.
I think Redirect doesn't work properly when the URL is already altered by a RewriteRule. How would you tackle this problem? Any help is appreciated!
You should exclude /seeds/ directory form general rules like this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
Note: clear browser cache then test
Also , don't use regex with redirect like what you did :
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
Here this (.*) has no meaning , you could use either RedirectMatch or RewriteRule

htaccess redirect subdomain to directory

I need help to write proper rewrite rules in my htaccess files.
I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.
I managed to do that successfully with this piece of code:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
My problem now is to write something more specific for pages, for example :
fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)
BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)
Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :
Redirect 301 /discover/foo /bar/otherfoo
Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo
How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.
Thanks !
EDIT
Please note that it's for a wordpress installation, and the htaccess starts with :
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First the these rules:
RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]
In order to capture bot www & non-www requests for subdomain.
Also this rule :
Redirect 301 /discover/foo /bar/foo
Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]
Note: clear browser cache then test.

301 redirect based on request domain 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]

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]

How to set htaccess to redirect/rewrite from to subdomain

I want htp://www.seostuff.org.ua/?s=seo to be redirected to htp://seo.seostuff.org.ua
Instead of seo can be any other search pattern
Buy this way I managed to make htp://seo.seostuff.org.ua redirecting to htp://www.seostuff.org.ua/?s=seo
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.seostuff\.org\.ua
RewriteCond %{HTTP_HOST} ^(.*)\.seostuff\.org\.ua [NC]
RewriteRule .* http://www.seostuff\.org\.ua/?s=%1 [L]
But I do not want URL change it should stay the same, ex htp://seo.seostuff.org.ua
I want revert requests to be processed as well (means htp://www.seostuff.org.ua/?s=seo should 301 redirect to htp://seo.seostuff.org.ua)
Also I do not need any slowness of processing such URL requests. Want to create optimized Rules. Any help please?
I would really appreciate this.
You cannot rewrite to a full HTTP URL without redirect to it.
So try this:
RewriteEngine on
# rewrite abc.seostuff.org.ua to abc.seostuff.org.ua/?s=abc
RewriteCond %{HTTP_HOST} !^www.seostuff.org.ua
RewriteCond %{HTTP_HOST} ^(.*).seostuff.org.ua
RewriteRule .* ?s=%1 [L]
# redirect seostuff.org.ua/?s=abc to abc.seostuff.org.ua
RewriteCond %{QUERY_STRING} ^s=([a-zA-Z0-9\-_]+)$
RewriteRule .* http://%1.seostuff.org.ua/ [L,R=301]

Resources