Port forwarding in apache with additional parameter - apache

How can I forward all incoming requests on one port say 8000 to another port say 8081, & add a static parameter in all request url while forwarding so that I can check for that specific parameter in my script?

Try with below rules in your root htaccess,
RewriteEngine On
RewriteCond %{SERVER_PORT} ^8000$
RewriteRule ^$ http://%{HTTP_HOST}:8081%{REQUEST_URI}?param=value [QSA,L]

Related

How to forward a request from one particular port to another with Apache httpd?

I have request that is http://localhost/services/pdf/module/generate/ to
http://localhost:8080/services/pdf/module/generate/
What to write in httpd.conf to do this.
You can use below redirect rule to forward request from particular port to another Apache httpd. You can adjust port as per your requirement.
RewriteEngine On
RewriteCond %{SERVER_PORT} ^80
RewriteRule ^/(.*) http://%{HTTP_HOST}:8080/$1 [L,R]

Apache .htaccess convert http uri to https causing redirect loop

I know this question has been asked a thousand times, but I cannot seem to find the answer.
We have a website hosted by 123-reg's shared web hosting package (no access to http config files). I have added ssl to the site, and the certificate works when directly requesting using https.
The problem arises when I try to redirect everything from http to https using the .htaccess file.
First I tried the SERVER_PORT variable in the condition:
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.co.uk/$1/ [R=301,L]
This does not work as the https redirect request uses port 80 also (I am querying this with 123-reg at the moment). The condition is always met and causes a redirect loop.
Next I tried the HTTPS variable:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://example.co.uk/$1/ [R=301,L]
This condition is always met as the variable is never set to on (and causes a redirect loop). I wonder if this is to do with the port no = 80 for https.
I found two server variables, SSL and HTTP_X_FORWARDED_SSL, which do change from "" to 1, but only when I delete the .htaccess file and directly request http or https.
If I try and use the SSL or HTTP_X_FORWARDED_SSL variables in the RewriteCond condition, it causes a redirect loop.
I cannot see the variables while the redirect loop is happening, so I do not know if they are being changed during the re-direct.
edit:
I have found the answer. I was using:
RewriteCond %{SSL} !1
which should be:
RewriteCond %{ENV:SSL} !1

Difference between 2 apache mod_rewrites

I've found 2 different code snippets to force https on my website:
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
and
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
I'm sure that they both work (one's from Httpd Wiki and the other's from SSL shopper). Would someone be able to explain the differences in how they perform the redirect?
They just use different Apache variables to make up the URL for redirect.
RewriteRule (.*) https://%{SERVER_NAME}/$1 [R,L]
This first rule takes the filename if one is entered such as myfile.php and appends the redirect with it replacing $1 in the redirect so that you get https://somesite.com/myfile.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
The 2nd one using %{HTTP_HOST} will grab the information from the http headers instead to make up the URL used to redirect so entering the same url http://somesite.com/myfile.php will be redirected to https://somesite.com/myfile.php
It's just a matter of telling apache what to use for redirection. Either use the server internal name or use the one sent by the browser.
%{SERVER_NAME}
That is a server internal variable in apache and is defined in the server config.
%{HTTP_HOST}
This is the what is sent by the browser in the HTTP request headers. This is client side while the SERVER_NAME if from the server config.
%{REQUEST_URI}
REQUEST_URI is the path component of the requested URI, such as "/index.html". This is a special Apache variable.
There a many ways that have been done to redirect to https and both should work. Your choice.

.htaccess Redirect and proxy ports

I have an apache server that is part of a cloud servers network.
The virtual server its configured to respond at port 9000, but you access from outside through port 80. Thats working fine, except for when I try to make a redirect from the .htaccess file
I have the next 2 lines to redirect errors to a folder index
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule .* /folder/ [R=301,L]
That tries to redirect the user to:
www.domain.com:9000/folder/, and it should be redirecting to www.domain.com/folder (which means, www.domain.com:80)
How can I avoid that?
thanks!
I solved it changing the rewrite rule to:
RewriteRule .* http://%{HTTP_HOST}/folder/ [R=301,L]

rewrite rule base on first sub domain to directory

I need to redirect the first sub domain of any domain name that will hit the root folder of that web server to a subfolder.
It need to keep the incoming port (stupid isp that block port 80 and 443).
It need to be able to handle both http and https.
It need to ignore www.whateverthedomain.ext
Ex.
dom1 : mydummy.com
dom2 : toberedirected.net
dom3 : putanydomainhere.ca
possible incoming url
http(s)://firstsub.mydummy.com:8082
http(s)://whateveryoutypehere.thegoodsub.toberedirected.net
http(s)://firstsub.mydummy.com/firstsub/
http(s)://www.mydummy.com/
The result of the 3 incoming url should be
http(s)://firstsub.mydummy.com:8082/firstsub/
http(s)://thegoodsub.toberedirected.net/thegoodsub/ (i dont care what is in front of the first sub)
http(s)://firstsub.mydummy.com/firstsub/ (dont rewrite, it is not the root folder)
http(s)://www.mydummy.com/ (dont rewrite, ignore www and give default webpage of the domain)
It is probably easy to write but i cannot figure it out this morning.
Try adding this to the htaccess file in your document root
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.
RewriteRule ^/?$ /%1/ [L,R=301]
This should preserve the hostname and port when redirecting. It will only redirect requests for the web root, and will redirect to the name of the subdomain, with a trailing /.