htaccess Rewrite URL based on TLD - apache

On my website, I am interested in moving my forums from a subdirectory to a subdomain. The forums are currently located at example.com/forums, and I want to move them to forums.example.com. After the move, I want to use htaccess to 301 redirect all forum traffic to the subdomain, but the problem is that I have two TLDs for my site, a .com domain and a .net domain.
I am currently trying to redirect traffic using this:
RewriteCond %{HTTP_HOST} !=forums.example.net
RewriteRule ^forums(/(.*))?$ https://forums.example.net/$2 [L,R=301]
RewriteCond %{HTTP_HOST} !=forums.example.com
RewriteRule ^forums(/(.*))?$ https://forums.example.com/$2 [L,R=301]
This only half-works. Regardless of which TLD I visit, it always redirects me to forums.example.net, even if I am visiting from example.com/forums, in which case I want it to go to forums.example.com. How could I achieve this?

You can use this singe rule as very first rule in forums/.htaccess instead of top level .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^.*$ http://forums.%1/$0 [L,R=301,NE]
For root .htaccess use this rule as your first rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?(example\.(?:com|net))$ [NC]
RewriteRule ^forums(/.*)?$ http://forums.%1$1 [NC,L,R=301,NE]

Please use below rules for the proper redirection to work as you want it.
Rewritengine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^forums$ http://forums.example.com/? [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.net$
RewriteRule ^forums$ http://forums.example.net/? [L,R=301]

Related

.htaccess redirect chain generated by multiple redirects

Wondering if you can help me?
I'm working on a site at the moment, and the following is in the .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule ^$ http://www.example/folder-name [R=301,L]
As I see it, this works in the following manner:
root domain rewrites using 301 to www. and then an additional 301 redirect to the /folder-name.
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
NB. there is a subdomain, let's call that products.example.co.uk - and access would be needed here.
Is this possible? my .htaccess skills are not particularly tight so any help gratefully received!
This should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/folder-name/$1 [L,R=301]
Ideally, I'd like to remove the middle redirect (i.e from root domain to www. and keep just the redirect from root domain to /folder-name.
This should do it :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.co.uk [NC]
RewriteRule ^((?!folder).*)$ http://www.example.co.uk/folder/$1 [L,R=301]

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Redirecting a domain with htaccess with a few specific 301s

Is it possible to do something like this to redirect old traffic to the new site with a 301.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]
However, I will need to manually map some pages to their new equivalent pages like
Redirect 301 /about http://new-example.com/about-us
Is it possible to do both?
If you are going to use Rewrite, then use it only. I don't recommend use both mod-alias and mod-rewrite. You can use rewrite to map to individual pages too. Also the order matters. The catchall rule should be the last one. This would provide cleaner code IMO.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^new-example.com$ [NC]
RewriteRule ^about/?$ http://new-example.com/about-us [R=301,L]
RewriteCond %{HTTP_HOST} !new-example.com$ [NC]
RewriteRule ^(.*)$ http://new-example.com/$1 [L,R=301]

htaccess wildcard redirect not working

I have an htaccess wildcard redirect on a website, it redirect another domain to a main one and also redirects www to none www with wildcard for the main domain. The following rules are made with cpanel, however it does not use the wildcard.
These same rules work fine on many other sites just not the one in question.
RewriteCond %{HTTP_HOST} ^www\.domain1\.ca$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^(.*)$ "http\:\/\/domain1\.ca\/$1" [R=301,L]
Any ideas? I am out of ideas for what could be causing this.
To add more clarification:
if i go to www.domain1.ca/some-page it should redirect to domain1.ca/some-page
However instead it goes to domain1.ca.
Tested in every browser, remade the rules, removed all cache and did multiple dns flush's, nothing has changed this.
You can use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTP_HOST} ^(www\.)?domain2\.com$ [NC]
RewriteRule ^ http://domain1.ca%{REQUEST_URI} [R=301,L,NE]

RewriteRule help

I have a URL
http://test.devsite-1.com/test/tbox/
which I want to redirect to
http://tbox.devsite-1.com/
Rule:
RewriteCond %{HTTP_HOST} !^tbox\.(.*)$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.|)(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/tbox(/.*|)$
RewriteRule /tbox/(.*) http://tbox.%{HTTP_HOST}/$1 [R=301,L]
I don't understand why it is not redirecting me to the URL? Please note I need a generalized rule so that if I change test.devsite-1.com to tempo.devsite-1.com the same should work with the other URL as well.
Try this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.(.+)$ [NC]
RewriteRule ^test/tbox/(.*)$ http://tbox.%1/$1 [R=301,L]
This will redirect (301 Permanent Redirect)
http://test.devsite-1.com/test/tbox/something-optional
to
http://tbox.devsite-1.com/something-optional
It has to be placed in .htaccess file in website root folder (e.g. http://test.devsite-1.com/.htaccess). If placed elsewhere some tweaking may be required.
It will only work if request is coming via test. subdomain.
And it will only work if URL requested starts with test/tbox/.
All of the above matches your URL examples.