I have an Apache2 Configuration that rewrites all urls to localhost:8080. I need one url for images that is not rewritten and instead serves the files from var/www/html (i.e. https://.../images -> var/www/html).
I know how do to that in nginx, but apache2 seems to to complicated for me. All efforts so far did not help, and neither did the official mod-rewrite-docs... Here is the current config. I guess it is just two lines at the right spot...
Thanks for your help...
.
.
.
ServerName ---.---.---
RewriteEngine On
RewriteCond %{HTTP:Upgrade} =websocket [NC]
RewriteRule /(.*) ws://localhost:8080/$1 [P,L]
RewriteCond %{HTTP:Upgrade} !=websocket [NC]
RewriteRule /(.*) http://localhost:8080/$1 [P,L]
RequestHeader set X-Forwarded-Proto "https"
RequestHeader set X-Forwarded-Port "443"
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
.
.
.
I have to redirect requests to 2 different servers(Server-A and Server-B) based on the request received on original apache server.
Server-A is also another apache server and Server-B is weblogic server. I am using weblogic proxy plugin directive to redirect requests for Server-B. So only requests which has context as /console or /mywlsapp should be redirected to weblogic server. All other requests should be redirected to Server-A.
I tried to exclude WLS contexts using ReWrite Cond. But it didn't worked.
This is my rewrite rules. Can you help me with this?
RewriteCond %{REQUEST_URI} !^/console/
RewriteCond %{REQUEST_URI} !^/mywlsapp/
RewriteCond %{HTTP_HOST} ^mycustomapp\.mycompany\.com:7090 [OR]
RewriteCond %{HTTP_HOST} ^http://mycustomapp\.mycompany\.com:7090
RewriteRule ^(.*) http://Server-A.mycompany.com/$1 [P]
<Location /console>
WLSRequest On
SetHandler weblogic-handler
WebLogicHost Server-B
WebLogicPort 7110
</Location>
<Location /mywlsapp>
SetHandler weblogic-handler
WebLogicHost Server-B
WebLogicPort 7112
</Location>
What I think you might want is this
RewriteCond %{REQUEST_URI} !^/console/
RewriteCond %{REQUEST_URI} !^/mywlsapp/
RewriteCond %{HTTP_HOST} ^mycustomapp\.mycompany\.com$
RewriteCond %{SERVER_PORT} ^7090$
RewriteRule ^(.*)$ http://Server-A.mycompany.com/$1 [R=301,L]
This should 301 redirect
http://mycustomapp.mycompany.com:7090/foo
to
http://Server-A.mycompany.com/foo
However, all of the above conditions must be fulfilled for this redirect to happen, in clear English:
URI may not begin with /console/ or /mywlsapp/ AND
Host has to be exactly mycustomapp.mycompany.com AND
Port has to be exactly 7090
Please note that e.g. http://mycustomapp.mycompany.com:7090/console will not be redirected, but http://mycustomapp.mycompany.com:7090/console/ will.
I am trying to remove the trailing slash from all of my URLs and redirect.
I've accomplished this here:
<Directory /var/www/html/ >
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ ${BaseUrl}/$1 [R=301,L]
</Directory>
However, I am also running a service locally and want to proxy to it visits /${ServiceRoot}.
ProxyPass /${ServiceRoot} http://localhost:${ServicePort}/
ProxyPassReverse /${ServiceRoot} http://localhost:${ServicePort}/
Each of these works fine individually. However, if I try to visit a URL like this: ${BaseUrl}/${ServiceRoot}/some/path/
The trailing slash is left on. I would like to force a redirect so the URL in the browser shows without the trailing slash.
Thanks in advance!
Just remove the L from the RewriteRule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ ${BaseUrl}/$1 [R=301]
That should apply the rule to your ProxyPass directive. Just remember to check DirectorySlash if you plan to support directory listings, it will try to redirect directories to / and then back in a loop.
You probably got something like this in your apache config:
ProxyRequests On
ProxyVia On
<Proxy *>
Order deny,allow
Allow from xx.xx.xx.xx
</Proxy>
Then you have to add the following in order to proxy-pass all requests to www.olddomain.com/foo to www.newdomain.com/bar
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule /foo(.*)$ http://www.newdomain.com/bar/$1 [P,L]
What this does is:
When a request is made to host www.olddomain.com the RewriteRule will fire
This rule substitutes /foo to http://www.newdomain.com/bar/
The substitution is handed over to mod_proxy (P)
Stop rewriting (L)
Example result:
Browser is configured to use your apache as proxy server
It requests www.olddomain.com/foo/test.html
Your apache will rewrite this to www.newdomain.com/bar/test.html
It will request this page from the responsible webserver
Return the result to the browser as www.olddomain.com/foo/test.html
I want to proxy all requests to Mongreel except for a few ruby apps that are running with fastcgi on apache.
So basically i have http://domain.com/ Mongreel app
http://domain.com/appa ruby app handled by apache
http://domain.com/app_testb ruby app handled by apache
My httpd.conf looks like this:
RewriteEngine On
RewriteCond $1 !^(appa|app_testb)
RewriteRule ^(.*)$ http://127.0.0.1:port/$1 [P]
But it fails.
http://doamin.com works as expected proxyed to Mongreel but the other 2 app are not handled by apache.
Any ideea what's wrong with my config?
UPDATE Or how can i enable mod_proxy for everything except /appa/* and /app_testb/* ?
The correct way is
RewriteEngine On
RewriteCond %{REQUEST_URI} !appa
RewriteCond %{REQUEST_URI} !appb
RewriteRule ^(.*)$ http://127.0.0.1:port/$1 [P]
RewriteConds do not see what's been matched in the rule
It seems i have found a way:
ProxyPass /appa !
ProxyPass /app_testb !
ProxyPass / http://127.0.0.1:port/
ProxyPassReverse / http://127.0.0.1:port/
How do you redirect HTTPS to HTTP?. That is, the opposite of what (seemingly) everyone teaches.
I have a server on HTTPS for which I paid an SSL certification for and a mirror for which I haven't and keep around for just for emergencies so it doesn't merit getting a certification for.
On my client's desktops I have SOME shortcuts which point to http://production_server and https://production_server (both work). However, I know that if my production server goes down, then DNS forwarding kicks in and those clients which have "https" on their shortcut will be staring at https://mirror_server (which doesn't work) and a big fat Internet Explorer 7 red screen of uneasyness for my company.
Unfortunately, I can't just switch this around at the client level. These users are very computer illiterate: and are very likely to freak out from seeing HTTPS "insecurity" errors (especially the way Firefox 3 and Internet Explorer 7 handle it nowadays: FULL STOP, kind of thankfully, but not helping me here LOL).
It's very easy to find Apache solutions for http->https redirection, but for the life of me I can't do the opposite.
Ideas?
This has not been tested but I think this should work using mod_rewrite
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
Keep in mind that the Rewrite engine only kicks in once the HTTP request has been received - which means you would still need a certificate, in order for the client to set up the connection to send the request over!
However if the backup machine will appear to have the same hostname (as far as the client is concerned), then there should be no reason you can't use the same certificate as the main production machine.
For those that are using a .conf file.
<VirtualHost *:443>
ServerName domain.com
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/domain.crt
SSLCertificateKeyFile /etc/apache2/ssl/domain.key
SSLCACertificateFile /etc/apache2/ssl/domain.crt
</VirtualHost>
Based on ejunker's answer, this is the solution working for me, not on a single server but on a cloud enviroment
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{ENV:HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If none of the above solutions work for you (they did not for me) here is what worked on my server:
RewriteCond %{HTTPS} =on
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]
all the above did not work when i used cloudflare, this one worked for me:
RewriteCond %{HTTP:X-Forwarded-Proto} =https
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
and this one definitely works without proxies in the way:
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
this works for me.
<VirtualHost *:443>
ServerName www.example.com
# ... SSL configuration goes here
Redirect "https://www.example.com/" "http://www.example.com/"
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com
# ...
</VirtualHost>
be sure to listen to both ports 80 and 443.
It is better to avoid using mod_rewrite when you can.
In your case I would replace the Rewrite with this:
<If "%{HTTPS} == 'on'" >
Redirect permanent / http://production_server/
</If>
The <If> directive is only available in Apache 2.4+ as per this blog here.
None of the answer works for me on Wordpress website but following works ( it's similar to other answers but have a little change)
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
If you are looking for an answer where you can redirect specific url/s to http then please update your htaccess like below
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/(home/panel/videos|user/profile) [NC] # Multiple urls
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /(home/panel/videos|user/profile) [NC] # Multiple urls
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It worked for me :)
As far as I'm aware of a simple meta refresh also works without causing errors:
<meta http-equiv="refresh" content="0;URL='http://www.yourdomain.com/path'">