Use variable in RewriteCond match - .htaccess

I'm running a site in 2 environments: production and development.
Each server has a vhosts.conf file, which sets environment variables. One of these variables is the correct subdomain to use.
vhosts.conf (development server)
RewriteRule .* - [E=DEV_OR_WWW:dev]
vhosts.conf (production server)
RewriteRule .* - [E=DEV_OR_WWW:www]
I also have a .htaccess file, which is identical on both servers. I am trying to rewrite old domain urls into new ones, while preserving the subdomain, like this:
.htaccess
RewriteCond "%{HTTP_HOST}" "%{ENV:DEV_OR_WWW}\.olddomain\.com" [NC]
RewriteRule "^/(.*)" "http://%{ENV:DEV_OR_WWW}.newdomain.com/$1" [L,R=301]
But the RewriteCond above does not work. %{ENV:DEV_OR_WWW} is not matching www or dev, and the rule is not applied. How can I use a variable to match the HTTP_HOST against?
If you need more details, or if my question is unclear, just ask.

You can use the following :
RewriteCond %{ENV:DEV_OR_WWW} ^(.+)$
RewriteCond %1.olddomain.com ^(www|dev)\.olddomain\.com$ [NC]
RewriteRule /(.*) http://%1.newdomain/$1 [L,R=301]

Related

RewriteRule in .htaccess and Reverse Proxy

Here is my problem, I`m struggling with it for a few days.
We`ve got a domain firstpart.maindomanin.com nad subdomain secondpart.maindomain.com.
Under first domain there is a first part of the project (based on SaaS commerce) and second part (under secondpart.maindomain.com) - based on Symfony. Those two parts are connected through SOAP services etc.
For firstpart.maindoman.com we are using Cloudflare.
We`ve got reverse proxy so:
firstpart.maindomain.com/uk/made is pointed to secondpart.maindomain.com/uk
and now (we cant enable cloudflare secondpart.maindomain.com due to some unrelated issues) we want to redirect all url-s from secondpart.maindomain.com/uk to firstpart.maindomain.com/uk/made
so for example
secondpart.maindomain.com/uk/furniture to firstpart.maindomain.com/uk/made/furniture
secondpart.maindomain.com/uk/sales to firstpart.maindomain.com/uk/made/sales
etc.
so we need to change domain and add 'made' between language code and rest of url
Other than that we need to redirect all urls like
firstpart.maindomain.com/uk/furniture to firstpart.maindomain.com/uk/made/furniture
(add 'made' between language code and rest of url)
and we need to do it in htaccess under subdomain secondpart.maindomain.com.
I came up with something with:
RewriteCond %{HTTP_HOST} secondpart.maindomain.com$ [NC]
RewriteRule ^([a-z]{2,3})(.*)$ http://firstpart.maindomain.com/$1/made$2 [R=301,L]
and for url like
secondpart.maindomain.com/uk/furniture
I`m getting redirection to
http://firstpart.maindomain.com/uk/made/furniture
which is fine but after redirection there is infinite loop (so except changing urls is not working)
As it turned out HTTP_HOST for both firstpart.maindomain.com/uk/made and secondpart.maindomain.com/uk is the same and it is secondpart.maindomain.com so condition is not working.
I came up also with condition like
RewriteCond %{REQUEST_URI} !^made [NC]
RewriteRule ^([a-z]{2,3})(.*)$ http://firstpart.maindomain.com/$1/made$2 [R=301,L]
so condition is not met if there is a word 'made' inside URI and in this case it is the same as in first rule.
I tried several different configurations but nothing is working.
When i tested it with http://htaccess.madewithlove.be/ everything was fine and there was not redirection.
So im assuming there is something with reverse proxy on cloudflare.
Im not an expert in htaccess but really i tried a lot of solutions and nothing is working.
I would really appreciate some help with it.
P.S. Just in case here is a .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteRule ^([a-z]{2,3})(.*)$ http://vendauat.lauraashley.com/$1/made$2 [R=301,L]
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
This condition:
RewriteCond %{REQUEST_URI} !^made [NC]
will always be met because your regex pattern says: if the request never starts with made, but the %{REQUEST_URI} variable always starts with /. Maybe what you want is this instead:
RewriteCond %{REQUEST_URI} !^/[^/]+/made/

htaccess help! wildcard subdomain rewrite rule to a specific folder for only some subdomains

Through a little research I came up with an htaccess rewrite rule in my development directory that takes a wildcard subdomain to its matching instance name in my development folder. I will admit I know little to nothing about this so help is really needed. So the current htaccess behaves as such:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
example
instance_folder_name.mydomain.com
redirects to
public_html/development/instance_folder_name
this works great because it allows me to have development websites in the development directory and i dont have to be constantly creating subdomains for each site.
the problem I am having is how to approach the following scenerio:
I use virtual domains as an add on component in Joomla so that several domains are being managed by one single Joomla install. So i may have the following subdomains...
client1.mydomain.com
client2.mydomain.com
client3.mydomain.com
that all need to go to
/public_html/development/client1
I guess what i need is a general rule on how to handle all wildcard subdomains, but with exceptions for client1, client2, client3 for example
You can have a separate rules for these 3 subdomains and add an exclusion condition in the older rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(client1|client2|client3)\. [NC]
RewriteRule ^((?!client1/).*)$ /client1/$1 [L,NC]
RewriteCond %{HTTP_HOST} !^(client1|client2|client3)\. [NC]
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.mydomain\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [L]

htaccess rewrite folder with different paths

I have an web application setup on a local and staging server. The local application has a different basepath to the staging. e.g.
Local
http://phils-imac.local/git/clients/myproject/html/
Staging
http://myserver.com/myproject/html/
I would like use htaccess to make the urls accessible without the 'html' part. e.g.
http://phils-imac.local/git/clients/myproject/
http://myserver.com/myproject/
I have been using this rewrite rule on my staging server:
RewriteEngine On
RewriteCond %{SERVER_NAME} =myserver.com
RewriteCond %{REQUEST_URI} !^/myproject/html/.*$
RewriteRule ^(.*)$ /myproject/html/$1 [L]
It works ok but I feel I need to customise it for each project. Ideally I'd like the 'myproject' part to be a variable and have the rule more general so it would also work on my local path.
You can't define variables in your .htaccess but if you keep your project name same you can accommodate for a variable base path in your rewrite rule as
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/html/ [NC]
RewriteRule ^(.*?)/?myproject/(.*)$ $1/myproject/html/$2 [NC,L]
This requires your .htaccess in your web root directory. If your .htaccess resides inside your myproject folder try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !/html/ [NC]
RewriteRule ^(.*)$ html/$1 [L]
In both the cases, you need to drop your %{SERVER_NAME} check as it would never match between your local and staging locations.

301 Redirect Rule gives different results on different hosting environment

I have been doing some URL rewriting for a customer in a test environment that work fine in my environment but when I move to theirs I get different results and the rule fails.
I am 301 redirecting legacy dynamic URLs in the following format:
http://www.companyname.com/page.php?pagename=AboutCompany&lang=EN
to
http://www.companyname.com/EN/about-company/
Using the ruleset below this works fine on my 'Heart Internet' shared hosting but fails on their 'Go Daddy' shared hosting (configs below). On the Go Daddy environment the legacy URL gets redirected to:
http://www.companyname.com/GET/about-company/
instead of
http://www.companyname.com/EN/about-company/
where does the 'GET' come? This is a barebones page with no other code influencing these rules.
RewriteCond %{QUERY_STRING} pagename=AboutCompany
RewriteCond %{QUERY_STRING} lang=([^&]+)
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page.php
RewriteRule ^page.php$ /%1/about-company/? [R=301,L]
RewriteRule ^([A-Za-z]+)/about-company$ page.php?pagename=AboutCompany&lang=$1 [NC,L]
RewriteRule ^([A-Za-z]+)/about-company/$ page.php?pagename=AboutCompany&lang=$1 [NC,L]
The 'lang' querystring differs depending on the user language preference (EN or FR or DE etc...)
Test Enviroment:
Windows
IIS7.5
PHP 5.3.6
Customer Enviroment:
Linux
Apache
PHP 5.2
Thanks, M.
where does the 'GET' come? This is a barebones page with no other code influencing these rules.
In this snippet of your rules, you have a backreference %1 in your RewriteRule. This references the last () match in the previous RewriteCond, which happens to be (GET|HEAD):
RewriteCond %{QUERY_STRING} lang=([^&]+)
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page.php
RewriteRule ^page.php$ /%1/about-company/? [R=301,L]
You want to switch those 2 conditions around:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /page.php
RewriteCond %{QUERY_STRING} lang=([^&]+)
RewriteRule ^page.php$ /%1/about-company/? [R=301,L]
So that the %1 references ([^&]+)

Optimize htaccess Wildcard Subdomain Code

I have Wildcard Subdomains working perfectly on my server, but would like to optimize the .htaccess code to make it more portable. I'm not sure how to do it.
I would like to replace the specific domain name and extension (DOMAIN and .com in the code below) with a regex match so that when using the wildcard code for other domains it is easy to port. So it should be a drop in replacement for domain.com and domain2.net. Here's the code I use:
# Wildcard Subdomain
RewriteCond %{HTTP_HOST} ^(www.)?([^.]+)\.DOMAIN\.com [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://DOMAIN.com/$1?a=%2 [R=301,QSA,L]
It's a little different than the usual version you see, because I've added the exception for the www. + subdomain condition. It many cases people enter www.sub.domain.com which breaks on the server. So I check for the www. and remove it before redirecting.
Thanks for your help!
The way you currently have it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteCond %2 !^www$ [NC]
RewriteRule ^(.*)$ http://%3.%4/$1?a=%2 [R=301,QSA,L]
If you just want to strip the www from the domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)\.(.*)\.(.*)\.(.*) [NC]
RewriteRule ^(.*)$ http://%2.%3.%4/$1 [R=301,QSA,L]

Resources