mod_rewrite Cannot redirect without rewriting url - apache

I'm trying to redirect to another server application running on a different port of the same box with apache HTTPD as the frontend server running on port 80. I'm using the rewrite engine and the following syntax to redirect calls:
RewriteRule ^/?products/(.*)$ http://www.example.com:9000/$1 [L]
This works great, but the url changes to port 9000 in the browser. Whenever I change the [L] to a [P] for proxy I get a 404, not found error in the browser.
How do I fix this so that the url doesn't change in the browser, but that it still works?

You need to make sure you have mod_proxy loaded, otherwise the P flag won't work.
You can also use the ProxyPass directive as part of mod_proxy in your vhost config.

Related

Jenkins links point to http instead of HTTPS resulting in login screen infinite loop

For some reason Jenkins is redirecting the login screen back to the login screen when a successful login is made. See the attached packet trace. If I give the expected URL using https instead of HTTP all the pages load fine.
I have Jenkins configured behind a reverse proxy using apache. The proxy redirects traffic at /jenkins to :8080/jenkins. The base url is set to https://domain/jenkins and the jenkins --prefix parameter is set to /jenkins. I appreciate any help!
I found a work around. There was an error in my rewrite in the http virtualhost in apache. With the extra slash removed Jenkins no works but it is still sending http packets into the proxy server which then need to be rewritten to https instead of just posting all https links. Works but there is still and issue with Jenkins unfortunately.
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}**/**$1 [R,L]
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
The slash in the Rewrite rule was causing the URL to be rewritten slightly wrong. Most web application I've used previously can tolerate this error evidently as I have only encountered this problem 3 years after initially setting up the server.

Apache redirect for Get call to some other server node

How can I do a get API call, to redirect to another sever in Apache using htaccess, or any other configuration?
I want to use some other server to process my requests to get more performance, without changing my application logic.
i.e : i have following:
localhot/user/getInformation to 192.169.1.1/user/getinformation
RewriteEngine On
RewriteRule ^/user/getInformation http://192.168.1.1/user/getInformation [NC,L,R=301]
This will do a permanent redirect (301) from /user/getInformation to http://192.168.1.1/user/getInformation.

Redirect Without changing URL Apache

I want to redirect one URL to another without changing the Browser URL
www.example.com/abc/(.*).xml should redirect to www.example.com/abc/xyz/index.htm?file=$1
But the Browser should display www.example.com/abc/(.*).xml
You can use a RewriteRule:
RewriteEngine On
RewriteRule /abc/(.*)\.xml$ /abc/xyz/index.htm?file=$1 [L]
Make sure you have mod_rewrite enabled and put this either in your VirtualHost config, or in a .htaccess file in your DocumentRoot
As Constantine posted on the accepted solution, the [P] flag is dangerous as it converts the server as a proxy.
See [this]: https://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever?noredirect=1&lq=1
P = Proxy. Forces the rule to be handled by mod_proxy. Transparently provide content from other servers, because your web-server fetches it and re-serves it. This is a dangerous flag, as a poorly written one will turn your web-server into an open-proxy and That is Bad.

Apache rewrite rule with proxy flag fail to work for mediawiki service

I have a local apache httpd hosting a mediawiki service that listen to port 3300. The service may access in my LAN via
http://mylan:3300/wiki/
I configure my internet router to add a port forwarding entry to the mylan:3300. I may then access to the wiki via something like:
http://<dynamic domain>:3300/wiki/
Next, I have a web site that serve globally. The web server is apache httpd too. I add a rewrite rule in .htaccess:
RewriteEngine On
RewriteRule ^/?wiki/?$ http://<dynamic domain>:3300/wiki/ [P]
I wish to access the wiki that host on my local LAN via the proxy method but using the global internet domain namespace:
http://<internet domain>/wiki/
After execute the above URL from internet browser, I can see the wiki contents. However, the URL shown in address bar change to
http://<dynamic domain>:3300/wiki/index.php/Main_Page
In order to test the proxy rewrite rule work, I try to change the rewrite rule to:
RewriteRule ^/?wiki/?$ http://www.google.com/search?q=test [P]
Open the URL:
http:///wiki/
lead me to google search page but the URL remain as http:///wiki/.
Any ideas why the rewrite rule
RewriteRule ^/?wiki/?$ http://<dynamic domain>:3300/wiki/ [P]
make the browser show the new URL address instead of internet domain name space:
http://<internet domain>/wiki/
A good example is:
RewriteRule ^/?wiki/(.*)$ http://en.wikipedia.org/wiki/$1 [P]
If we access the url:
http://<domain>/wiki/Country
The URL will always rewrite and shown as
http://en.wikipedia.org/wiki/Country
Instead, I expect it to show as
http://<domain>/wiki/Country
but the content is from http://en.wikipedia.org/wiki/Country
Probably a redirect that includes the full link. To rewrite them add something like:
ProxyPassReverse /wiki/ http://<dynamic domain>:3300/wiki/
I also notice in the Apache online documentation for URL Rewriting Guide - Advanced topics in the section on Content Handling, sub section Dynamic Mirror has this example:
RewriteEngine on
RewriteBase /~quux/
RewriteRule ^hotsheet/(.*)$ http://www.tstimpreso.com/hotsheet/$1 [P]

How to redirect a page and disguise its url?

I need to do the following on my apache webserver :
Redirect any URL starting with :
http://mydomain1.com/archive
to
http://mydomain2.com/archive
Is there a way using mod-rewrite or RewriteEngine to disguise that URL, so that the URL that appears in the browser is mydomain1.com ? I don't want to give away the fact that we are switching servers.
you could try a reverse proxy. This will allow you to take one url and forward the request to another server without the end user knowing.
Try adding this in the .htaccess from domain1.com/archive
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule (.*) http://mydomain2.com/archive [P]
</IfModule>
Edit: Tell me if you've tried this and if it worked or not.
That's not possible using mod_rewrite. If host changes then it has to be an external redirect using R flag.
A possible workaround is to make server to server call from inside your code on mydomain1 to mydomain2. If using php you can use file_get_contents function to make this happen.