apache2 force proxy for specific url on a subdomain - mod-rewrite

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?

Related

i get access forbidden from Apache (httpd) in fedora 34, Laravel, SELinux

I have configured httpd in my fedora and I checked everything is true but i get
access forbidden
I have tested everything about .htaccess but still getting access forbidden. also, my laravel folder permissions are correct.
I tested a sample project while installing httpd it worked perfectly with no 'access forbidden' message.
this is my httpd config
<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
DocumentRoot /home/myuser/gitlab/register/public
<Directory /home/myuser/gitlab/register/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
this is my htaccess in laravel public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
your probably creating the issue by mixing old Apache syntax with new Apache syntax, modern versions of Apache should be something like this...
<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
DocumentRoot /home/myuser/gitlab/register/public
<Directory /home/myuser/gitlab/register/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
# Order allow,deny
# Allow from all
</Directory>
</VirtualHost>
I found the problem.
it is because of the
SELinux
ubuntu does not uses SELinux by default but RedHat distributions use it by default. SELinux prevents apache to access the home directory for the sake of security so if you disable or put it to Permissive mode, apache can access your home directory to load the web project. if you do not want to disable SELinux so the correct location for web applications is in /var/www/ folder
so if we change httpd config to
<VirtualHost *:80>
ServerName test.com
ServerAlias www.test.com
DocumentRoot /var/www/gitlab/register/public
<Directory /var/www/gitlab/register/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
it works fine

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

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.

wildcard sub-domain on localhost using codeigniter

I have setup virtual host on my localhost by using codeigniter and it is working fine. But I am not able to create wildcard subdomains. To add virtual host below are my steps on windows machine.
C:\Windows\System32\drivers\etc\hosts.txt
In this host.txt I have added below line
127.0.0.1 localhost.com
I am using xampp .I have added below lines in httpd-vhosts.conf file.
D:\xampp\apache\conf\extra\httpd-vhosts.conf
<VirtualHost localhost.com:80>
DocumentRoot "D:/xampp/htdocs/adeptra"
ServerName localhost.com
ServerAlias localhost.com
<Directory "D:/xampp/htdocs/adeptra">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "D:/xampp/htdocs/adeptra"
ServerName localhost.com
ServerAlias *.localhost.com
<Directory "D:/xampp/htdocs/adeptra">
Options Indexes FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
Below is my .htaccess code to remove index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
So now my question is, is it really possible to create wildcard subdomains in localhost? If yes, then please help me in this regard. Any help would be greatly appreciated.
A similar question has already been asked and answered here. Basically, no you cannot create subdomains on localhost. You need to create a local domain which is explained in the linked answer.

How to add a redirection for additional domain

I have a server with the following virtual host defined :
<VirtualHost *:80>
DocumentRoot /dev/null
ServerName www.originaldomain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^([^.]+\.)?originaldomain.com
ReWriteRule ^.*$ http://www.destinationdomain.com [L,QSA,R=301]
</VirtualHost>
This successfully redirects www.originaldomain.com and originaldomain.com to www.destinationdomain.com.
However I now to additionally redirect www.originaldomain2.com and originaldomain2.com to destinationdomain.com. As an Apache novice I am unsure how best to achieve this. Any advice appreciated.
Add a ServerAlias for each of your new domains.
ServerAlias www.originaldomain2.com
ServerAlias originaldomain2.com

Setting up Wildcard subdomain (with reverse proxy) on apache 2.2.3

What I am trying to achieve is the following:
I want to have numerous subdomains such as abc.domain.com redirect to a url such as www.domain.com/something?subdomain=abc
Since I am redirecting to a fully qualified domain, I needed to use a reverse proxy to avoid the change of the URL in the browser. (using the [P] Flag and turning on the mod_proxy module and some other modules)
This is my DNS setup
*.domain.com. 14400 A 111.111.11.1
This is my virtual host configuration for apache
<VirtualHost 111.111.11.1:80>
ServerName www.domain.com
ServerAlias *.lionite.com
DocumentRoot /var/www/html
ErrorLog /var/www/logs
UseCanonicalName off
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule ^(.+) %{HTTP_HOST}$1 [C]
RewriteRule ^([^.]+)\.domain\.com(.*) http://www.domain.com/something?subdomain=$1 [P,L]
This setup is working fine (Let me know if you think you can improve it of course).
My main problem is when I am trying to setup https://
This is my virtual host configuration for apache
<VirtualHost 111.111.11.1:443>
ServerName www.domain.com:443
ServerAlias *.domain.com
DocumentRoot /var/www/html
SSLEngine on
SSLProtocol all -SSLv2
SSLCipherSuite ALL:!ADH:!EXPORT:!SSLv2:RC4+RSA:+HIGH:+MEDIUM:+LOW
SSLCertificateFile /etc/httpd/conf.d/cert/server.crt
SSLCertificateKeyFile /etc/httpd/conf.d/cert/server.key
<Directory "/var/www/cgi-bin">
SSLOptions +StdEnvVars
</Directory>
SetEnvIf User-Agent ".*MSIE.*" \
nokeepalive ssl-unclean-shutdown \
downgrade-1.0 force-response-1.0
CustomLog logs/ssl_request_log \
"%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/images
RewriteCond %{HTTPS_HOST} !^www\.domain\.com$
RewriteRule ^(.+) %{HTTPS_HOST}$1 [C]
RewriteRule ^([^.]+)\.domain\.com(.*) https://www.domain.com/something?subdomain=$1 [P,L]
</VirtualHost>
Whenever I call https://abc.domain.com - the response I am getting is the homepage but no matter what I am appending to the end of the subdomain, I will get the same response. It's like the rewrite isn't responding well.
Any help would be appreciated, or if you could share how you'd setup reverse proxy, rewrite, wildcard subdomain and SSL all together
Thanks,
I have had this same problem as well. The only way I solved it was to put different domains that need secure connection on different Listen ports because I was limited with IP addresses.
From my understanding, the problem is that in the https protocol the HOST is not included in the request. So when the request reaches the server, apache just uses the first match on the IP and port the connection was received on because it does not know the domain it was requested from.
The only work around for this is to have a different IP for each domain, or a different port.
Unfortunately you are out of luck using https with a wildcard domain setup, I don't believe there is anyway to get it to work.

Resources