advanced .htaccess mod_rewrite from domain to subdirectory - .htaccess

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.

Related

htaccess how to redirecting multiple domains in all variations to one domain with https

Here is my .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.vibratape\.de$ [NC]
RewriteRule ^ https://www.vibratape.de%{REQUEST_URI} [R=301,L,NE]
There are this domain...
vibra-tape.de
vibra-tape.com
vibratape.com
vibratape.de (this is the goal domain)
Every combination work except one. If you enter a none goal domain with https... exmpl.
https://www.vibratape.com

.htaccess wildcard redirect from parked domain?

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]

.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)

.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]

Creating a mod_rewrite rule in an htaccess file

I am trying to create a mod_rewrite rule to direct people to a sub-folder. Currently the code looks as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
The redirect works if the user types www.abcsite.com, but not if they type abc.com. Is there something that I am missing or should do differently to make sure the user goes to the correct folder (regardless of how they type the URL)?
Side note: The htaccess file that I am dealing with is a Joomla file, so all contents of it deal with another Joomla site. I appreciate the help.
Because you have conditions for that.
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
All above rules will pass only its abcsite.com
You add following rules also then it work for abc.com too.
RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
There's a stray ! in your second condition. A ! in front of the pattern means that the condition is true when the regex doesn't match (like in the third condition). A ! inside the pattern is just a literal symbol.
The host conditions should be something like:
RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]
And in fact, they can be joined into a single condition (note, no [OR] here):
RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]
Your third condition is intended to prevent redirect loops (/foo → /abc/foo → /abc/abc/foo → …). What it says is that the rule isn't applied if the request URL starts with /abc/. However, your actual redirect is an internal redirect: if a user accesses abcsite.com/foo, the server internally rewrites this to /webroot/abc/foo, but REQUEST_URI stays the same, /foo.
The reason this doesn't cause a redirect loop as it is is likely rewrite rules in abc/.htaccess which override this one once the redirect is done.
What should be checked instead in the third condition is the path matched by the rewrite rule:
RewriteCond $1 !^abc/
RewriteRule (.*) /abc/$1 [L]

Resources