apache2 rewrite rule without changing URL - apache

This below rewrite redirects localhost to http://www.example.com/?id=211&test=1 but I want that localhost on browser should not be changed but the page will come form the above link.
I am using this rewrite rule on my Apache conf:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^localhost$ [OR]
RewriteRule ^/?$ http://www.example.com/?id=211&test=1 [L]

This below rewrite redirects localhost to
http://www.example.com/?id=211&test=1 but I want that localhost on
browser should not be changed but the page will come form the above
link.
If you want to load a page from elsewhere without chaining the URL, mod_rewrite is the wrong tool for the job. Use mod_proxy instead. First enable it in Apache like this; example assumes you are on Ubuntu 12.04 but should work on most any Linux Apache install
sudo a2enmod proxy proxy_http
Then set this to enable a reverse proxy from your root path of / to http://www.example.com/?id=211&test=1:
<IfModule mod_proxy.c>
# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.example.com/?id=211&test=1
ProxyPassReverse / http://www.example.com/?id=211&test=1
</IfModule>
EDIT: Seems like mod_proxy and query strings for the destination do not mix; emphasis mine:
This directive allows remote servers to be mapped into the space of
the local server; the local server does not act as a proxy in the
conventional sense, but appears to be a mirror of the remote server.
The local server is often called a reverse proxy or gateway. The path
is the name of a local virtual path; url is a partial URL for the
remote server and cannot include a query string.
So if there is anyway you could set another page—maybe on localhost—that would bounce it behind the scenes. Meaning this happens on localhost:
ProxyPass / bounce.php
And then the file, bounce.php could have this line in it:
<?php
header('Location: http://www.example.com/?id=211&test=1');
?>
Which would allow mod_proxy to have a valid destination. And then the PHP file does the redirect? Hard to say, but the query string on your destination server is the issue.

Related

URL Rewrite in httpd.conf

I have a redirect setup on my Apache server within the httpd.conf file that redirects all traffic to "server.mydomain.com" to https://server.mydomain.com/uri. Unfortunately, some users will enter https before the URL and it does not redirect, instead they get Apache error page. I want to know how can I get users who input https before the URL to be redirect to https://server.mydomain.com/uri. I believe I may have to do a rewrite but I'm not sure or know how to go about doing it. I've research about rewrite and found it should be done in the .htaccess file but when I read Apache best practice they state it should not be done within .htaccess file for security and performance. Instead, it should be done within the config file. Since the redirection is working within the httpd.conf file, I would like to incorporate the rewrite there as well. I presume that is the correct according to Apache website. My issue is how do I go about doing this within that file. I've included the file information below. Any assistance is greatly appreciated. The server is using Tomcat 7 with Apache 2.2.15.
httpd.conf file:
</IfModule>
#
ProxyPass /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
ProxyPassReverse /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
</IfModule>
<VirtualHost *:80>
ProxyPass /uri http://server.mydomain.com:8080/uri
ProxyPassReverse /uri http://server.mydomain.com:8080/uri
Redirect permanent / https://server.mydomain.com/uri
</VirtualHost>
Did you try creating a vhost for 443 and switching to http there via Rewrite?
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

mod_jk and mod_rewrite with prefix

We are trying to configure apache to forward requests to different servers hosting different Application servers.
We want to achieve the following.
www.mydomain.com/server1 --->forward to ---> 172.30.34.50:8082 (AP1 jboss)
www.mydomain.com/server2 --->forward to ---> 172.30.34.51:8082 (AP2 jboss)
Our current configuration:
mod_jk.conf:
JkOptions +ForwardKeySize +ForwardURICompat -ForwardDirectories
httpd.conf
JkMount /cliente1* ajp13unsecure
RewriteEngine on
RewriteLog logs/apache-mod_rewrite.log
RewriteLogLevel 3
RewriteRule ^/cliente1(/)?([^/]*)/?$ /$2 [L,PT] (tried w/o PT with no success)
The problem is that when mod_jk forwards the request, it gets to the application servers like this: 172.30.34.50:8082/server1
Which is not a valid resource in the application server, it should go to root (172.30.34.50:8082)
We tried using mod_rewrite, but it triggers before mod_jk, so when apache try to match mod_jk rule, it doesn't satisfy the condition anymore. And the request is not forwarded.
How can we get mod_rewrite to trigger right before mod_jk does the forwarding so the application servers get the correct URI.
Or is there a way to configure mod_jk to forward request without the context?
How can we get mod_rewrite to trigger right before mod_jk does the
forwarding so the application servers get the correct URI.
Not too sure why I have noticed an uptick in people asking about mod_jk, but in my experience mod_proxy works better & is easier to understand for doing what you are attempting to do.
To enable mod_proxy in Apache do the following; assuming you are on Ubuntu/Debian:
sudo a2enmod proxy proxy_http
Then restart Apache like this; again assuming you are on Ubuntu/Debian:
sudo service apache2 restart
That done, this is a simple setup that should work within your Apache config. Winging it based on your settings:
# Settings for adding a trailing slash to the URL
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(server1|server2)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}$1/ [R=301,L]
# Settings for Apache Reverse Proxying
<IfModule mod_proxy.c>
# Proxy specific settings
ProxyRequests Off
ProxyPreserveHost On
<Proxy *>
AddDefaultCharset off
Order deny,allow
Allow from all
</Proxy>
ProxyPass /server1 http://172.30.34.50:8082/
ProxyPassReverse /server1 http://172.30.34.50:8082/
ProxyPass /server2 http://172.30.34.51:8082/
ProxyPassReverse /server2 http://172.30.34.51:8082/
</IfModule>
The initial mod_rewrite settings add a trailing slash to URLs which I found I needed to do in cases where a path fragment—like /server1 and /server2—were going through a reverse proxy.
Also note I have /server1 and /server2 set but they might need to have a slash added to them like this /server1/ and this /server2/. Experiment to see what works best.

Apache SSL Proxypass gives unsecured content for zope

Had hit a roadblock with the apache configuration trying to redirect or proxy to other webservers. Have google for quite a while but am stuck as mentioned in the scenario below. Apart from using apache for some out of the box web applications like Plone, cmdbuild, etc., I don’t have much experience with apache. Leaving security aside for the configuration for Apache, am I trying to achieve something the wrong way. Do give me your views and other possible ways to achieve what I am trying to do. Am planning to run all the sites under https.
Scenario:
Server 1 – 10.0.0.1 (CentOS 6.5)
Apache (version 2.2.5) running on port 80 (SSL enabled) – Main Web Server
Zope (Plone) running on port 8080
Sites:
http://10.0.0.1:8080/site1
http://10.0.0.1:8080/site2
Server 2 – 10.0.0.2 (CentOS 6.5)
Tomcat running on port 8080
Sites: http://10.0.0.3:8080/site3
Server 3 – 10.0.0.3 (Windows Server 2008)
IIS running on port 80
Sites: http://10.0.0.4/site4
If ProxyPass and Proxy PassReverse is used on the apache ssl.conf like below:
ProxyPass /site1 http://10.0.0.1:8080/site1
ProxyPassReverse /site1 http://10.0.0.1:8080/site1
ProxyPass /site2 http://10.0.0.1:8080/site2
ProxyPassReverse /site2 http://10.0.0.1:8080/site2
ProxyPass /site3 http://10.0.0.2:8080/site3
ProxyPassReverse /site3 http://10.0.0.2:8080/site3
ProxyPass /site4 http://10.0.0.1:8080/site4
ProxyPassReverse /site4 http://10.0.0.1:8080/site4
Site 3 and Site 4 are working properly fine with the ssl enabled but site 1 and site 2 (running under localhost zope) are working too but the contents are not showing properly because
“Connection Partially Encrypted
Parts of the page you are viewing were not encrypted …..”
If RewriteEngine is used instead of ProxyPass as shown below, both site1 and site2 runs perfectly under ssl.
RewriteEngine On
RewriteRule ^/(.*) http://10.0.0.1:8080/VirtualHostBase/https/%{SERVER_NAME}:443/site1/VirtualHostRoot/$1 [L,P]
But the other sites site3 and site4 are not reachable.
Apache httpd.conf just had a permanent redirect to https
<VirtualHost *:80>
Redirect permanent / https://10.0.0.1/
</VirtualHost>
If your rewrite rule is working for anything other than site1, it's because acquisition is making other sites appear inside site1. You don't want to depend on that.
Instead, have a separate rewrite rule for each site:
RewriteEngine On
RewriteRule ^/site1(.*) http://10.0.0.1:8080/VirtualHostBase/https/{SERVER_NAME}:443/site1/VirtualHostRoot/$1 [L,P]
RewriteRule ^/site2(.*) http://10.0.0.1:8080/VirtualHostBase/https/{SERVER_NAME}:443/site2/VirtualHostRoot/$1 [L,P]
RewriteRule ^/site3(.*) http://10.0.0.2:8080/VirtualHostBase/https/{SERVER_NAME}:443/site3/VirtualHostRoot/$1 [L,P]
RewriteRule ^/site4(.*) http://10.0.0.1:8080/VirtualHostBase/https/{SERVER_NAME}:443/site4/VirtualHostRoot/$1 [L,P]
Don't try to do this with one rewrite rule, or you'll expose your Zope root via proxy. A bad idea.

mod_rewrite: redirect requests from localhost to remote server

I have a following scenario:
Remote server with some webapp running at http://remote/webapp
Local machine inside corporate network
Corporate proxy between them
Apache with mod_rewrite running on my local machine
I would like to have the mod_proxy rewrite every request like http://localhost/webapp?someparams into http://remote/webapp?someparams.
Currently I have the following httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
<Directory />
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Which results in mod_rewrite transforming http://localhost/webapp?someparams into http://remote/C:/Apache2.2/htdocs/webapp?someparams
How to configure mod_rewrite to handle it correctly?
Since it looks like you have access to vhost/server config, you should ditch mod_rewrite and just use mod_proxy:
ProxyPass /webapp http://remote/webapp
ProxyPassReverse /webapp http://remote/webapp
and get rid of the 2 mod_rewrite lines (which is redirecting, not proxying):
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Note that if you have cookies, you may need to reverse map their domains.using ProxyPassReverseCookieDomain.
Also:
The fact that windows absolute path appears in the URL is due to misconfiguration of the mod_rewrite and this is what I'm trying to avoid
This is not a misconfiguration with mod_rewrite. When you put rewrite rules inside a <Directory>, the filepath is used in the match instead of the URI-path. According to the mod_rewrite documentation
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
Thank you Jon for inspiration, finally mod_proxy + mod_rewrite worked:
# Global context
RewriteEngine On
RewriteRule ^(.+) http://remote/$1 [P]
ProxyPassReverse / http://remote/
I know that this is a simplified and coarse solution, but works for my purpose.

Content Fetching Using htaccess

Let I have two domains named www.abc.example and www.xyz.example hosted in different servers.
I have a .htaccess file in the root directory of www.abc.example (i.e. www.abc.example/.htaccess)
What will be the .htaccess script if I want to load the contents of www.xyz.example when I request from www.abc.example.
As for example:
If I browse www.abc.example/test then it will display the content of www.xyz.example/test and so on without changing the host url (i.e. www.abc.example) in browser's addressbar.
What you're trying to do is run a reverse proxy. It requires mod_proxy on the server.
The documentation seems to suggest you'd want something like:
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://www.xyz.example/
ProxyPassReverse / http://www.xyz.example/
Assuming that such configuration is allowed in .htaccess. If not, you will have to use mod_rewrite and a RewriteRule with the [P] flag:
RewriteRule ^/(.*) http://www.xyz.example/$1 [P]
If you're going the RewriteRule route, don't forget to add RewriteEngine On if it isn't there already!