RewriteRule in .htaccess and Reverse Proxy - .htaccess

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/

Related

re-direct a domain to a sub-domain + make it https

this is kinda an odd one:
I need my site to do two things (one of which is already working):
if a user tried to access the domain via HTTP:// it is replaced with https:// - this is for SEO in google and to make the user feel more secure -
the site folder that is used to load the website needs to be the subdomain folder of the site
Oddly the second part of this is working and I figured out - however I'm not sure how to merge these two requests:
HTACCSESS
RewriteEngine on
RewriteCond %{HTTP_HOST} ^trippy\.co\.nz$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.trippy\.co\.nz$
RewriteCond %{REQUEST_URI} !update.trippy.co.nz/
RewriteRule (.*) /update.trippy.co.nz/$1 [L]
But I'm not sure how to make the site display as
https://trippy.co.nz/
I have tried:
RewriteEngine On
RewriteCond %{HTTP_HOST} update\.trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://update.trippy.co.nz/$1 [R,L]
but then the web address displays as: https://update.trippy.co.nz
and I need to remain as https://trippy.co.nz/
Any help here would really great and I know its a odd situation to be in.
THanks,
Wally
...but then the web address displays as: https://update.trippy.co.nz
You would seem to be redirecting to the subdomain itself, not the subdomain's subdirectory, as you appear to be doing in the first rule. You may also be putting the directives in the wrong order - the external redirect needs to go first - otherwise you are going to expose the subdomain's subdirectory, which does not appear to be the intention.
Try the following instead:
RewriteEngine On
# Redirect HTTP to HTTPS
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
# Rewrite all requests to the subdomain's subdirectory
RewriteCond %{HTTP_HOST} ^(www\.)?trippy\.co\.nz [NC]
RewriteRule !^update\.trippy\.co\.nz/ /update.trippy.co.nz%{REQUEST_URI} [L]
No need for the extra condition in the 2nd rule block, as the check can be performed directly in the RewriteRule and use the REQUEST_URI server variable instead of the $1 backreference in the substitution string.
That that R by itself is a temporary (302) redirect. You may want to change that to R=301 (permanent) once you have confirmed this is working OK.

redirect all external requests to the prod equivalent of the server url .htaccess

I'm trying to say:
For all IP Addresses that are not within the 110.140 or 110.10 ranges. If they are trying to access a URL that begins with "stage." then redirect them to the same URL but remove the "stage." portion of the string from the url.
RewriteCond %{REMOTE_ADDR} !^(110\.(140|10)) [NC]
RewriteCond %{HTTP_HOST} ^stage\. [NC]
RewriteRule stage\.(.*) https://$1 [R=301,L]
When using just the last two lines in made with love htaccess tester the last line fails (is not met). I haven't tested the code on a server.
Update:
After reading the Apache RewriteRule Directive details, I realized that the RewriteRule Directive does not search the HTTP_HOST, only the things after that. Therefore this approach will not work. Does anyone have an approach that will work?
Looks like your hostname starts with stage. not the URI. You may use this rule:
RewriteCond %{REMOTE_ADDR} !^110\.(140|10)
RewriteCond %{HTTP_HOST} ^stage\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]

Forced SSL in htaccess going to only the home page

I have recently added an SSL to my sites. I have added the code to the .htaccess file to force the https. The issue is that my external links that go to pages within the site are now being redirected to the homepage. The code I am using is:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonelec.com%1 [R,L]
I think the issue is in the last line, as the rule is telling it to redirect to the homepage. What I can't seem to find is a rule that will say for it to go to the URL provided in the link but give it an https instead of the HTTP.
I did do a search for this topic, but all the code I found was similar to what I already had. Thank you for all your help.
Update
I have two sites I am trying to work this out for, watsonenerysolutions.com and watsonelec.com.
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^(.*)$ https://www.watsonenergysolutions.com/$1 [R,L]
It still sent to the homepage
When I tried
RewriteOptions InheritDownBefore
RewriteCond %{ENV:HTTPS} !on [NC]
RewriteRule ^ https://www.watsonenergysolution.com%{REQUEST_URI} [R,L]
I received an error message that said Safari can't open the page "https://www.watsonenergysolutions.com/index.php" because Safari can't find server "www.watsonenergysolutions.com"
%N backreferences are what you match in RewriteCond's. In your case, it is empty. That's why anything is going to the homepage.
You need to use $1 or %{REQUEST_URI}, both rules below are equivalent (the second may be faster because you don't -re-match unnecessarily)
RewriteRule ^(.*)$ https://www.watsonelec.com/$1 [R,L]
RewriteRule ^ https://www.watsonelec.com%{REQUEST_URI} [R,L]
Note 1: %{REQUEST_URI} value always begins with a leading /, while what you can match in a RewriteRule never begins with a leading /
Note 2: R flag uses a 302 redirect by default. Maybe you'll want to use a 301 ([R=301,L])

Root redirects to just one subdomain in .htaccess

I want the root of my website (www.bitumephotofest.it) to redirect to a subdomain (2016.bitumephotofest.it) (I am running a Wordpress multisite). It works.
But I have another subdomain (2015.bitumephotofest.it) and it also redirects to 2016.bitumephotofest.it.
I want the redirect to work only between www.bitumephotofest.it and 2016.bitumephotofest.it. 2015.bitumephotofest.it should be independent as it is a different website.
I tried to look for questions by people with a similar situation but there is always something different and, anyway, those solutions does not work for me.
Here is my code:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} 2016.bitumephotofest.it
RewriteCond %{REQUEST_URI} !wordpress/
RewriteRule ^(.*)$ /wordpress/$1 [L]
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteRule !^(2016) http://2016.bitumephotofest.it [L,R]
Does anyone know what I am missing?
Thank you in advance!
If you want the domain 2015.bitumephotofest.it stay unmodified, you can sort of exit the rewrite rule chain with
RewriteCond %{HTTP_HOST} ^2015\.bitumephotofest\.it$
RewriteRule ^ - [L]
- as the target means don't rewrite, see RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.

RewriteRule [L] still changing the url

I found myself with this problem, which is driving me a little bit crazy. I use apache's mod_rewrite for pretty URLs and I need to use dynamic subdomains in the site. Everything is great and all the server has de wildcards. I use the next code on my .htacess:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/%1 [L]
The only problem is, even if I use the [L] flag the url of the site change to http://mysite.com/subdomain. What i want is the url to be like http://subdomain.mysite.com
The link mysite.com/subdomain is a dynamic url and is solved with another rule with the following code:
RewriteRule ^([A-Za-z]+)$ filter.php?type=subdomain&subdomain=$1
Any help would be appreciated
If you specify an external URL (which changing the subdomain does), a header redirect will take place. I don't think you can prevent that. But why not skip that step altogether, and use the second RewriteRule straight away?
I can't test this right now, but something like
RewriteCond %{HTTP_HOST} !^www.mysite.com
RewriteCond %{HTTP_HOST} ([^.]+).mysite.com [NC]
RewriteRule ^(.*)$ filter.php?type=subdomain&subdomain=$1
should work.

Resources