I have a situation I’ve not come across before that calls for some interesting mod_rewrite rules and I cant find any examples of someone trying to achieve the same thing in a similar configuration.
Currently I have two domain names which are configured to share the same document root, in said document root is a dynamic php application which, based on the incoming hostname, displays content specific to the that domain.
The domains for example purposes are:
www.example1.com
and
www.example2.co.uk
(one being a TLD the other not)
In addition to this application there are two wordpress installations one for each of the two domain names. As we are not using wordpress MU here I need some fancy rewrites to firstly hide the wordrpess folder, and secondly present the request to the correct folders based on the HTTP_HOST.
Currently I have the following:
RewriteRule ^wp-content(.*) wordpress/example1$1 [L]
RewriteRule ^wp-admin(.*) wordpress/example1/wp-admin$1 [L]
RewriteRule ^wp-login.php$ wordpress/example1/wp-login.php [L,R=301]
And similar rules for content specific pages.
This works well for the single wordpress installation, but obviously not for the second, what I was hoping to do here was something like the following:
RewriteRule ^wp-admin(.*) wordpress/${HTTP_HOST}/wp-admin$1 [L]
However I need to remove the www. and .com from the ${HTTP_HOST} variable (or the www. and .co.uk )
Any suggestions on a way to achieve this or a better approach would be appreciated.
You can use RewriteCond to check for a pattern in HTTP_HOST and then capture part of that pattern.
For instance:
The RewriteCond directive above checks to see whether HTTP_HOST fits a domain pattern ending “.com” or “.co.uk” and optionally beginning with “www.”. If it does, it captures the interesting part of the domain name.
Then the RewriteRule (which only fires if the RewriteCond does match) is able to refer to the captured part of the RewriteCond pattern by using the
%1
back-reference.The pattern I’ve used in the RewriteCond above might not suit your needs perfectly, but once you know you can use a back-reference to a pattern captured by RewriteCond, it should be easy for you to use this to get the effect you need.