How to get mod rewrite condition "if variable is not" to work - mod-rewrite

I've installed Oracle Apex and trying to deploy multiple applications with a apache reverse proxy but I cannot get apache to limit a virtual host to one application id.
The virtual host
<VirtualHost *:80>
ServerName www.example.com
ServerAlias test.example.com
ServerAlias example.com
ProxyPass / http://127.0.0.1:8080/ords/
ProxyPassReverse / http://127.0.0.1:8080/ords/
RewriteCond %{QUERY_STRING} !^p=101$
RewriteRule ^/$ f?p=101 [L,P]
<Location /i>
ProxyPass http://127.0.0.1:8080/i
ProxyPassReverse http://127.0.0.1:8080/i
</Location>
</VirtualHost>
What im trying to do is if the variable p is not 101 I want the proxy to redirect the user /f?p=101 so only the application that I want on that virtual host is accessible and not the other application but I wont work.
If I do /f?p=102 it will show the other application and not redirect me to application 101 again.
Does anyone know how to fix this?

I found the solution:
RewriteCond %{QUERY_STRING} ^f?p=100 [OR]
RewriteCond %{QUERY_STRING} ^f?p=4550 [OR]
RewriteCond %{QUERY_STRING} ^f?p=4000
RewriteRule ^/(.*) https://%{HTTP_HOST}/f?p=101:1 [R]
So what I am doing is checking if we get the application ID I need to filter out.
After that I just redirect the page to the original page for the application.

Related

Apache not rewriting url properly

I am setting up a reverse proxy with apache for a program that uses kestrel.
I attempted in httpd.conf:
<Location /asf>
ProxyPass http://localhost:1242/
ProxyPassReverse http://localhost:1242/
RewriteEngine On
RewriteRule ^asf/$ http://localhost:1242/$1 [R=301,L]
</Location>
and in .htaccess, placed in htdocs (root of site):
RewriteEngine On
RewriteRule ^asf/$ http://localhost:1242/$1 [R=301,L]
however neither of them has worked.
<Location /asf>
ProxyPass http://localhost:1242/
ProxyPassReverse http://localhost:1242/
</Location>
by itself in httpd.conf works without problems, but however api requests do not make it to the desired location.
I expect requests sent to example.com/asf/api/X to go to localhost:1242/api/X.
In addition to this, /api/NLog is a websocket, if that matters.

Apache 2.4 proxy add request parameter to all requets

I am using Crafter CMS with multi-tenancy. I am trying to setup Apache2.4 on RHEL7 to be a reverse proxy. http://site.example.com -> ajp://localhost:9009/?crafterSite=site
Here is my Apache2 virtual host configuration. I have ensured that mod_proxy and mod_rewrite are loaded. I can reach Crafter Delivery through the proxy but the rewrite isnt working as Crafter doesnt know what site I am trying to load. Does anyone have any suggestions on how to get this working.
<VirtualHost *:80>
ServerName site.example.com
LogLevel alert rewrite:trace3
RewriteEngine On
RewriteRule ^$ /?crafterSite=site [QSA,L]
<Proxy *>
Order allow,deny
Allow from all
</Proxy>
ProxyRequests Off
ProxyPreserveHost On
ProxyPass / ajp://localhost:9009/
ProxyPassReverse / ajp://localhost:9009/
</VirtualHost>
Try changing the rewrite rule to be:
RewriteRule (.*) $1?crafterSite=site [QSA,PT]
Where site is your site ID.
The differences are:
It rewrites anything coming in regardless of URL and preserves it (see the (.*) and $1)
It's a passthrough PT (not a redirect). This means it augments the request with the param and lets it straight through to Crafter Engine.

Http to Https AWS Elasticbeanstalk

I am using AWS Elasticbeanstalk for my Spring MVC web application. I want to redirect all the request to https. I tried following this How to force https on elastic beanstalk? but this didn't work for me. This code redirects to https but my app didn't work. It shows "This page isn’t working". Code for your reference
<VirtualHost *:80>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} =http
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<Proxy *>
Order Allow,Deny
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/ retry=0
ProxyPassReverse / http://localhost:8080/
ProxyPreserveHost on
ErrorLog /var/log/httpd/elasticbeanstalk-error_log
</VirtualHost>
Assuming you've already tested HTTPS working fine when your website is visited with HTTPS already. If not you can add this file .ebextensions/loadbalancer-terminatehttps.config with content as below:
option_settings:
aws:elb:listener:443:
ListenerProtocol: HTTPS
SSLCertificateId: arn:aws:acm:us-west-2:<your-account-id>:certificate/<certificate-arn-on-aws-acm>
InstancePort: 80
InstanceProtocol: HTTP
All what's left is to configure the instances Apache config to redirect the clients visiting your website with HTTP to HTTPS, add the code below to a new file .ebextensions/001_ssl_rewrite.config
Apache 2.4+
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
RewriteEngine On
<If "-n '%{HTTP:X-Forwarded-Proto}' && %{HTTP:X-Forwarded-Proto} != 'https'">
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
</If>
Apache 2.2.X
files:
"/etc/httpd/conf.d/ssl_rewrite.conf":
mode: "000644"
owner: root
group: root
content: |
LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
You can check which Apache is installed on your Elastic Beanstalk from here
For more, please read both of those answers: https://stackoverflow.com/a/38751749/1772245 and https://stackoverflow.com/a/40670047/1772245

Apache2 log not displayed for rewrite module and no rewrite

I've VPS in OVH and i developped website with j2ee and i associate apache with tomcat using mod_jk.
when i enter url http://ip_of_vps/myapp, i enter to website.
until now it's ok
now i add rewrite for access to website using only ip_of_vps without context (myapp), for this i'm using Rewrite in apache2.config like this :
<virtualhost *:80>
RewriteEngine On
LogLevel alert rewrite:trace6
RewriteRule ^/$ http://ip_of_vps/myapp/ [P,L]
</virtualhost>
And i change All AllowOverride in directory from none to All
when i try to access to http://ip_of_vps, i've apache2 home page.
what i've forgot ?
I'm using ubuntu 14.04 server, apache2, tomcat7.
Off the top of my head, typically you would write
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
ReWriteRule your rule
</IfModule>
so, is mod_rewrite actually loaded?
do you need your RewriteBase to be /myapp/ ?
my suggestion would be to put a .htaccess in your html root directory
with a rule like
RewriteRule ^myapp/(.*)$ $1 [L]
this way you wont need mod_proxy at all
hope this helps

apache2 force proxy for specific url on a subdomain

I have a site that has dynamic virtual subdomains using mod_rewrite, as defined like this:
<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
DocumentRoot /var/www/example.com/www
RewriteEngine on
RewriteCond %{HTTP_HOST} ^[^.]+\.examle.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.example.com(.*) /var/www/example.com/$1$2
</VirtualHost>
The problem is that I want a specific url, say subdomain.example.com/CONTROL/ to point back to www.example.com/ using a proxy (not url redirecting).
I have tried adding:
RewriteRule ^([^.]+)\.example.com/CONTROL(.*) /var/www/example.com/www$2 [P]
But that didn't work. Any ideas?

Resources