Apache rewrite of subdomain based on environment - regex

I'm trying to come up with a rewrite rule that will work in conf files for all the environments I support (qa, stage, dev, etc), so a common rewrite condition and rule to "rule" them all.
Example:
subdomain.environment.site.com should redirect to environment.site.com
except subdomain.site.com should redirect to www.site.com.
It also shouldn't forward when some of the request URIs have stuff in them.
This is as far as I've come:
RewriteCond %{HTTP_HOST} subdomain.$ENV.site.com [NC]
RewriteCond %{REQUEST_URI} !/images
RewriteCond %{REQUEST_URI} !/blah
RewriteCond %{REQUEST_URI} !/blah-foo
RewriteCond %{REQUEST_URI} !/foo-bar
RewriteRule ^/(.*) http://$ENV.site.com/$1 [L,R=302]
Obvious the $ENV isn't going to work, but I am having a having a helluva time trying to find a proper regex to make this work in all environments.

A little more research, hand banging and beer got me the solution.
It might not be pretty but it works. Answering so if anyone ever tries to do this, they have some sort of start here.
RewriteCond %{HTTP_HOST} ^(sub)(\.)((?:[a-z][a-z]+))(\.)(site)(\.)(com) [NC]
RewriteCond %{REQUEST_URI} !/image
RewriteCond %{REQUEST_URI} !/blah
RewriteCond %{REQUEST_URI} !/foobar
RewriteCond %{REQUEST_URI} !/barfoo
RewriteRule ^/(.*) http://%3.%5.%7/$1 [L,R=302]

Related

Htaccess Redirect Loop Issue

For particular reasons, I'm hosting a subsite within a parent site, and would like to use a domain alias for the subsite. For clarity, let's call the two domains parent.org and child.org.
The subsite is located at: parent.org/child
Since child.org is a domain alias, it's also located at: child.org/child
I'd like to build redirects that route any requests to either child.org or parent.org/child to child.org/child
Scouring the forums, I've attempted a number of things, though this one seemed the closest to what I'm going for:
RewriteCond %{HTTP_HOST} ^(www.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/.*$ [NC]
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L,NS]
RewriteCond %{HTTP_HOST} ^(www.)?parent\.org/child$ [NC]
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L,NS]
This doesn't quite work as hoped, resulting in child.org/child/child/child/etc.
What am I missing here?
Addendum:
To help clarify my goal here, for either domain, if "/child" isn't the first segment of the URL, I'd like to use parent.org. If "/child" isn't the first segment of the URL, I'd like to use child.org. So:
Parent setup:
parent.org/foo -> parent.org/foo
parent.org/child/foo -> child.org/child/foo
Child setup:
child.org -> child.org/child
child.org/foo -> parent.org/foo
child.org/child/foo -> child.org/child/foo
First RewriteCond %{HTTP_HOST} ^(www.)?parent\.org/child$ [NC] is wrong because %{HTTP_HOST} is representing either parent.org or child.org and could not match uri after them.
And i think you have no issue with child.org because it is already done before by domain alias which means that the request to child.org will show up every thing in child directory unless you do another configuration .
So , your code should looks like this , to redirect only when parent domain is involved :
RewriteCond %{HTTP_HOST} ^(www\.)?parent\.org$ [NC]
RewriteCond %{REQUEST_URI} ^/child/(.*)$ [NC]
RewriteRule ^child/(.*)$ http://child.org/$1 [R=302,L]
So , this part parent.org/child/ is already configured as child.org so any request contains parent.org/child/whatever will be child.org/whatever
Update : you siad Since child.org is a domain alias, it's also located at: child.org/child if that so ,the code should look like this :
RewriteCond %{HTTP_HOST} ^(www\.)?parent\.org$ [NC]
RewriteCond %{REQUEST_URI} ^/child/(.*)$ [NC]
RewriteRule ^(.*)$ http://child.org/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/(.*)$ [NC]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^(.*)$ http://parent.org/$1 [R=302,L]
RewriteCond %{HTTP_HOST} ^(www\.)?child\.org$ [NC]
RewriteCond %{REQUEST_URI} !^/child/(.*)$ [NC]
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*)$ http://child.org/child/$1 [R=302,L]
So , any request to parent.org/child/whatever will be redirected to child.org/child/whatever
Then any request to child.org/whatever and wherever is everything except child will be redirected to parent.org/whatever
Finally , any request to child.org will be redirected to child.org/child/

How to properly write a RewriteCond for https and subdomains

Thus far I've gotten this far with my RewriteConditions but it's from hacking and slashing from internet examples. They seems to behave a little funny at times, like showing the directory in the url, so I assume I'm not doing it right.
The desired behaviour: I have multiple subdomains all pointing to the same root. Based on the subdomain value I want to redirect to a sub-folder in the root. I also want to force HTTPS. I have multiple subdomains, but I'll only show two for the sake of brevity. All the other conditions are virtually identical.
Here's my .htaccess code:`
RewriteEngine On
RewriteCond %{HTTP_HOST} purchase.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !purchase/
RewriteRule (.*) https://purchase.mydomain.com/purchase/$1 [L]
RewriteCond %{HTTP_HOST} booking.mydomain.com [NC]
RewriteCond %{REQUEST_URI} !booking/
RewriteRule (.*) https://booking.mydomain.com/booking/$1 [L]
`
Thank you,
Mike
Change your rules like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(purchase|booking)\.mydomain\.com$
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{HTTP_HOST} =purchase.mydomain.com
RewriteRule ^((?!purchase/).*)$ /purchase/$1 [L]
RewriteCond %{HTTP_HOST} =booking.mydomain.com
RewriteRule ^((?!booking/).*)$ /booking/$1 [L]
http:// or https:// in target will redirect instead of rewriting.

Using RewriteRules on query strings from root of domain

I'm trying to rewrite the URLs on my site (to improve SEO, its an AJAX site) so that they go from
http://www.domain.co.uk/?section=home
http://www.domain.co.uk/?section=contact
to
http://www.domain.co.uk/home
http://www.domain.co.uk/contact
respectively.
This is my first time using .htaccess for rewriting, and I'm finding that I end up just guessing with my expressions/copying answers from other questions and trying to make them work.
The closest I've gotten is from adapting this answer here
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)section=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /section/%3?%1%4 [L,R=301]
RewriteRule ^section/(.*) index.php?section=$1 [L]
which gives me the result
http://www.domain.co.uk/section/contact
however this also breaks all my other links, and leaves the site without CSS or javascript etc.
Furthermore, one of the sections of my site has URLs that look like this
http://www.domain.co.uk/?section=blog&post=post-name-one
http://www.domain.co.uk/?section=blog&post=post-name-two
which I wanted to rewrite to
http://www.domain.co.uk/blog/post-name-one
http://www.domain.co.uk/blog/post-name-two
However, I'm am very unsure about how to approach this in RegEx.
Could anyone help in getting this right? If there are good references for writing these rules, I'd be happy to hear about them as well. So far the things I have read have been helpful in explaining what is happening, but I haven't been able to write my own yet.
You can use these rules in your root .htaccess:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+\?section=([^\s&]+)&post=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
RewriteCond %{THE_REQUEST} \s/+\?section=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /?section=$1&post=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ /?section=$1 [L,QSA]

Redirect folder to subdomain and keep subdomain working

Iv'e got such a stupid question here but here we go:
I've got this url: http://www.mysite.com/subsite. I would like to use "/subsite" like this: http://subsite.mysite.com. This is easily achieved with this code:
RewriteRule ^subsite(.*) http://subsite.mysite.com$1 [L]
But here's the stupid part. All my links on my site is already like this: "http://subsite.mysite.com/content". In this case this url should be redirected to "http://www.mysite.com/subsite/content". This creates endless loops (of course).
RewriteRule ^subsite(.*) http://subsite.mysite.com$1 [L]
RewriteCond %{HTTP_HOST} ^subsite\.mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/subsite/$1 [R]
Is it possible to achieve what i'm aming at?
You need to enable mod_proxy for this.
Then have rules like this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?mysite\.com [NC]
RewriteRule !subsite http://subsite.mysite.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTP_HOST} ^subsite\.mysite\.com [NC]
RewriteRule ^ http://www.mysite.com/subsite%{REQUEST_URI} [L,P]

drupal multisite htaccess: rewrite all languages to default language on one subsite

I have a Drupal 6 multisite, with 2 domains (www.example.com and www.domain.com), sharing some common content.
The domain example.com is in three languages (EN, FR, NL). Languages are set by path prefix (/en, /fr, /nl). The other domain domain.com is just in one language (NL).
The problem: on many occasions domain.com is shown in the wrong language, even if no path prefix is filled in. Somehow it seems to default to EN, though it doesn't always do that - behaviour doesn't seem to be very consistent.
The solution (at least I hope): since I'm not a Drupal developer (I inhereted the site from a former colleague) I have no idea how to fix this in Drupal, so I thought the best way to fix it would be to add some rewrite rules to .htaccess.
I'm no htaccess/regex expert either, and can't get it working. You can find my current rewrite rules below, any help or suggestions are most welcome.
Some examples:
www.domain.com/fr/some-title needs to be rewritten to www.domain.com/nl/some-title
www.domain.com/node/1975 needs to be rewritten to www.domain.com/nl/node/1975
These are the rewrite rules that were already there:
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I tried adding this:
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$ [NC]
RewriteRule ^/(.*)$ /nl/$1
and would expect this just to prepend /nl/ to all paths (thus not being a full solution since /fr/some-title would become /nl/fr/some-title) - however, a quick test shows me that:
/fr/some-title is rewritten to /nl/some-title (which is what I need, but not what I expected)
/some-title is not rewritten
The question: any ideas what might be wrong? Or could this be caused by other (Drupal) settings? Or is there a better way to solve my problem?
Just for the sake of completeness: the live website is www.cinemazuid.be
If this rule
RewriteRule ^/(.*)$ /nl/$1
is in your .htaccess file, I am surprised that it works as the leading / is always stripped out, so it should theoretically never match any request.
If your desire is to force a default language of NL for those requests that do not specify a language, then add the following rules to the top of your .htaccess file, before any existing rules
#if request is for existing file or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
#then stop processing
RewriteRule .* - [L]
#replace fr with nl. This rule
RewriteRule ^fr/(.*)$ /nl/$1 [L,R=301]
#if the request does not have a language of en or nl
RewriteCond %{REQUEST_URI} !^/(en|nl)/ [NC]
#redirect with nl as default language
RewriteRule .+ /nl%{REQUEST_URI} [L,R=301]
If you do not want to redirect, just drop the R=301
I edited code above to replace /fr/some-title with /nl/some-title/.
The L flag tells mod_rewrite to stop processing further rules, which is usually what you want, unless you have another rule that needs to further process the current request.
#redirect /fr/* and /en/* to /*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^(en|fr)/(.*)$ /$2 [R,L]
#internally rewrite /* to /nl/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond $1 !^nl/$ [NC]
RewriteRule ^(.*)$ /nl/$1
#drupal code
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]