.htaccess wildcard redirect from parked domain? - .htaccess

I have two domains pointing to my site, for example; http://some-long-domain.co.uk and http://short.co - I want to be able to share URLs like http://short.co/x/ic5rai, while redirecting all traffic to the www. version of the main domain, and keeping the path on the URL.
To complicate matters, I don't want to touch versions of the domain with a subdomain that I use for development (local. and staging.).
E.g.;
http://short.co/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://www.short.co/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://some-long-domain.co.uk/x/ic5rai -> http://www.some-long-domain.co.uk/x/ic5rai
http://local.some-long-domain.co.uk/x/ic5rai -> http://local.some-long-domain.co.uk/x/ic5rai
http://staging.some-long-domain.co.uk/x/ic5rai -> http://staging.some-long-domain.co.uk/x/ic5rai
Current re-write rules look like the following, and work for the latter point (local/staging), but not for anything else. What am I doing wrong?
RewriteCond %{HTTP_HOST} !^www.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} !^local.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} !^staging.some-long-domain.co.uk$ [NC]
RewriteCond %{HTTP_HOST} ^short.co$ [NC]
RewriteCond %{HTTP_HOST} ^www.short.co$ [NC]
RewriteRule ^(.*)$ http://www.some-long-domain.co.uk/$1 [R=301,QSA,L]

Related

Redirect 301 whole domain together with some specific URL's

I have two domains:
newdomain.com
and
olddomain.com
I need to redirect all links from olddomain.com to the main page of newdomain.com so it should look like this:
olddomain.com/irrelevantDir1/irrelevantPage1.html -> newdomain.com
olddomain.com/irrelevantDir11/irrelevantPage11.html -> newdomain.com
But there are some links that i want to save:
olddomain.com/relevantDir1/relevantPage1.html -> newdomain.com/newRelevantDir1/newRelevantPage1.html
olddomain.com/relevantDir11/relevantPage11.html -> newdomain.com/newRelevantDir11/newRelevantPage11.html
I already spent two days tried to make RewriteRule but with no luck, i can really use some help here.
You can write your rules from specific to general case handling like this:
RewriteEngine On
# specific URL redirect 1
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^relevantDir1/relevantPage1\.html$ http://newdomain.com/newRelevantDir1/newRelevantPage1.html [NC,L,R=301]
# specific URL redirect 2
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^relevantDir11/relevantPage11\.html$ http://newdomain.com/newRelevantDir11/newRelevantPage11.html [NC,L,R=301]
# generic case
RewriteCond %{HTTP_HOST} ^(?:www\.)?olddomain\.com$ [NC]
RewriteRule ^ http://newdomain.com/ [L,R=301]

advanced .htaccess mod_rewrite from domain to subdirectory

I've found a few threads with similar issues, but haven't had any luck modifying the answers to try and meet the requirements of my site. I have a primary domain, and re-write rules in place to redirect www domain to non-www domain, mapped like this:
primarydoamin.uk => primarydomain.uk
www.primarydomain.uk => primarydomain.uk
RewriteCond %{HTTP_HOST} ^primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$
RewriteRule ^(.*)$ primarydomain.uk/$1 [R=301,L]
I've a number of add-on / mapped domains against my hosting and I'd like to rewrite based upon a pattern:
drop 'www'
rewrite to a sub-directory of the primary domain (with periods '.' replaced to dashes '-')
only execute when the above rule, redirecting www domain to non-www domain traffic is not met, for example:
www.fishandchips.co.uk => primarydomain.uk/closed/fishandchips-co-uk
www.mushypeas.com => primarydomain.uk/closed/mushypeas-com
pukkapie.uk => primarydomain.uk/closed/pukkapie-uk
Currently I'm copying and pasting the following logic:
RewriteCond %{HTTP_HOST} ^(www.)?fishandchips.co.uk$
RewriteRule ^(.*)$ http://primarydomain.uk/closed/fishandchips-co-uk [R=301,L]
Your first set of rules can be resumed as:
RewriteCond %{HTTP_HOST} ^(www\.)?primarydomain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk%{REQUEST_URI} [R=301,L]
For your second set of rules I would try something like:
# This one will work for domains with single extension like .com
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2%{REQUEST_URI} [R=302,L]
# This one will work for domains with 2 extensions like .co.uk
RewriteCond %{HTTP_HOST} ^www\.([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC,OR]
RewriteCond %{HTTP_HOST} ^([^\.]+)\.([^\.]+)\.([^\.]+)$ [NC]
RewriteCond %{HTTP_HOST} !primarydomain.uk$ [NC]
RewriteRule ^ http://primarydomain.uk/closed/%1-%2-%3%{REQUEST_URI} [R=302,L]
The %{REQUEST_URI} at the end will ensure it passes down any URL it was coming from that domain.
NOTE: Keep in mind this is an untested solution. It should work as expected, however it does not take into account multilevel sub domains into it, which you will have to adjust as you go.
Also I have set it to 302 instead of 301, use 302 until you confirm its fully working then move it to 301 and also make sure you're using a different browser or cleared your cache to make sure you're not viewing a cached redirect.

.htaccess determine the HTTP_HOST

I have a multi-tenant site that spans across 3 different domain names. So for example:
client1.website.com
client2.website.net
client3.website.us
etc
In PHP I can use $_SERVER['HTTP_HOST'] to get the host name and determine if they are on website.com, website.net, etc.
I want to do the same in my .htaccess file as I have a couple of rewrite conditions.
Here they are:
RewriteCond %{HTTP_HOST} ^regionapi\.website\.com
RewriteCond %{HTTP_HOST} !^website\.com$ [NC]
Can I do something like this?
domain = %{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^regionapi\.{domain}
RewriteCond %{HTTP_HOST} !^{domain}$ [NC]
I might be way off but it would be great if this can work.
You should be able to do this by using something such as the following in your .htaccess:
RewriteCond %{HTTP_HOST} (www\.)?regionapi\.example\.(com|net|us)$ [NC]
RewriteRule ^(.*)$ http://example.%2/$1 [R=301,L]
Depending on the domain name extension regionapi.example.(com,net,us) would redirect to:
http://example.(com,net,us)

Multi-site force www. on ALL subdomains htaccess snippet

I previously got some great help here with a tad bit complicated .htaccess file.
It is a multi-site/domain file and uses %{HTTP_HOST} to rewrite/301 the non-wwww to the www.widgets.com address.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is that RegexFu only looks for the NOT www. and then adds the www.
This could cause some duplicate content issues if someone links to say, mail.widgets.com as this is redirected to www.mail.widgets.com.
To summarize:
I need:
mail.widgets.com -> www.widgets.com
dogs.widgets.com -> www.widgets.com
www.mail.widgets.com -> www.widgets.com
www.dogs.widgets.com -> www.widgets.com
etc.
I have tried a variety of permutations liek this with no luck:
RewriteCond %{HTTP_HOST} !^www\.[^\.]+\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
No dice. I either end up with a loop or some other problem.
I need simply anything that isn't:
www. (anything but period) .com - > www. (last string before .com) .com
and of coarse the standard:
(anything but period).com -> www. (anything but period).com
If someone could lend me a hand I would greatly appreciate it.
Try:
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.[^.]+\.[^.]+\.[^.]+ [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.com$ [NC]
RewriteRule ^(.*)$ http://www.%1.com/$1 [R=301,L]
The %1 backreferences the grouping that matches ([^.]+) in the previous rewrite condition. It ignores any subdomain that's before the domain name, if there is one.

.htaccess Wildcard Redirect Fix

I am using the following code to redirect wildcard subdomains (*.domain.com) to their coresponding folder in /users and redirect direct requests to the /users folder to the subdomain version:
Protect Direct Access to Wildcard Domain Folders:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
Handle Wildcard Subdomain Requests:
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule ^(.*)$ /users/%1/$1 [L]
This code works well enough, however there are two problems that I can't seem to fix.
The below scenario seems to happen because there isn't a trailing slash on the requesting URI:
username.domain.com/sub1 => username.domain.com/users/username/sub1
username.domain.com/sub1/ => username.domain.com/sub1/
The users directory can still be accessed directly by using a subdomain:
username.domain.com/users/username/sub1 => Works and shouldn't
I'm at a loss and would really appreciate if anyone has any ideas.
Thank you!
For the problem 2, I think your first protection rule just needs to redirect all subdomains. It's redirecting www, but lets username.domain.com come through as-is.
This will redirect any direct access request to the users path.
RewriteCond %{HTTP_HOST} ^.+\.domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
I think it can be a little simpler by just looking for any host ending in domain.com (which would even handle no subdomain, just domain.com) (I didn't test this....)
RewriteCond %{HTTP_HOST} domain\.com$ [NC]
RewriteRule ^users/([a-z0-9\-_\.]+)/?(.*)$ http://$1.domain.com/$2 [QSA,NC,R,L]
For problem 1, I'm stumped too, sorry. It's acting like the trailing slash is failing the rules, so it falls through as-is. But I would expect it to do as you want it to:
username.domain.com/sub1/ => username.domain.com/users/username/sub1/
Perhaps try the %{REQUEST_URI} tag, instead of trying to capture .*. I don't see the difference, but maybe it'll help.
RewriteCond %{REQUEST_URI} !^/users/ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteCond %1 !=www [NC]
RewriteRule .* /users/%1%{REQUEST_URI} [L]

Resources