mod_rewrite in Virtual Hosting For Reverse Proxy - mod-rewrite

I'm using Tomcat on port 8080 behind Apache 2.4 on port 443. I've setup the usual reverse proxy configuration on Apache virtual host which is working okay. This is my setup:
<VirtualHost *:443>
ServerName www.example.com
ServerAlias www.example.com
ServerAdmin admin#example.com
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://example.com:8080/
ProxyPassReverse / http:example.com:8080/
<Location />
Order allow,deny
Allow from all
</Location>
SSLEngine on
SSLCertificateFile /etc/ssl/example.com.crt
SSLCertificateKeyFile /etc/ssl/example.com.key
SSLCertificateChainFile /etc/ssl/CA.crt
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]
LogLevel alert rewrite:trace6
RewriteRule ^post$ post.xhtml [NC]
</VirtualHost>
I want to use mod_rewrite on Apache, however, this seems not to be working. I realized that when doing rewrite on .htaccess (which is not applicable in my case), the rewrite rules work okay. However, when I try the same rule from the virtual host, it is not working. For example, using the rule below:
RewriteEngine on
LogLevel alert rewrite:trace6
RewriteRule ^test$ test.php [QSA]
I get the 404 error with the following from the error logs:
[Tue Aug 14 12:12:40.135059 2018] [rewrite:trace2] [pid 11554] mod_rewrite.c(482): [client 127.0.0.1:53326] 127.0.0.1 - - [localhost/sid#7efc776e9518][rid#7efc776710a0/initial] init rewrite engine with requested uri /test
[Tue Aug 14 12:12:40.135108 2018] [rewrite:trace3] [pid 11554] mod_rewrite.c(482): [client 127.0.0.1:53326] 127.0.0.1 - - [localhost/sid#7efc776e9518][rid#7efc776710a0/initial] applying pattern '^test$' to uri '/test'
[Tue Aug 14 12:12:40.135121 2018] [rewrite:trace1] [pid 11554] mod_rewrite.c(482): [client 127.0.0.1:53326] 127.0.0.1 - - [localhost/sid#7efc776e9518][rid#7efc776710a0/initial] pass through /test
What could be missing? I wish it were possible to do rewrite rule on .htaccess

Here is the solution; first of all, .htaccess does not apply. According to the documentation, server config and virtual host are the context for mod_proxy that apply (.htaccess in our case doesn't make sense anyway).
The solution is to use a rewrite rule with the flag P, which causes the request to be handled by mod_proxy, and handled via a proxy request. See more here.
The virtual host should be modified as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^/post/(.*)$ post.xhtml\?id=$1 [P]
ProxyPass / http://example.com:8080/
ProxyPassReverse / http:example.com:8080/

Related

Laravel project routes not working when app is accessed over the network

when I access the application from same computer where wampServer is installed every thing works fine whether I access it using localhost or Ip of the computer but when I put wamp server online and access the same app from another computer using the Ip of the computer where wamp is installed routes doesn't work for me only root page is being accessed and if I click on any other link I get 404 error
Here is .htaccess file in public folder
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteBase /laravel/itman/public/
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]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
httpd-vhosts.conf file in wamp\bin\apache\apache2.4.41\conf\extra\httpd-vhosts.conf
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "${INSTALL_DIR}/www"
<Directory "${INSTALL_DIR}/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
The apache virtual host should point to the public directory for your laravel project.
e.g.
<VirtualHost *:80>
ServerName localhost
ServerAlias localhost
DocumentRoot "${INSTALL_DIR}/www/public"
<Directory "${INSTALL_DIR}/www/public">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Laravel with https can't open on production (site can't be reached)

I am deploy Laravel on ubuntu 18.04 apache to production with SSL certificate.
When i open the web on server's browser with https (i.e https ://example.co), the web open perfectly with valid certificate.
But when i open on other PC (with different network) using https://example.co, i got this error
This site can’t be reached
Check DNS_PROBE_FINISHED_NXDOMAIN
but if i use http://www.example.co on other PC (with different network), the web is ok and can working.
here is my conf in /etc/apache2/site-enabled/laravel.conf
<VirtualHost *:443>
ServerName 10.xx.x.xxx
ServerAlias www.example.co
DocumentRoot /var/www/laravel/public
SSLEngine On
SSLCertificateFile "/etc/ssl/certs/domain.crt"
SSLCertificateKeyFile "/etc/ssl/private/domain.key"
SSLCertificateChainFile "/etc/ssl/certs/intermediate.crt"
Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains"
<Directory /var/www/laravel/public>
DirectoryIndex index.php
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName 10.x.xx.xxx
DocumentRoot /var/www/laravel/public
<Directory /var/www/laravel>
AllowOverride All
</Directory>
</VirtualHost>
here is my .htaccess on /laravel/public/.htaccess
<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>
.env
APP_DEBUG=false
APP_URL=https://example.co
is there configuration that i've missing?

404 error when calling api in laravel 6.4 with apache2 virtualhost

I have Laravel 6.4 installed on ubuntu 18.04 with php 7.2 and apache2. I have built a case study to develop an api and when I execute the php artisan serve command and use postman to do the query everything works fine. But if I create a virtualhost, I can see the website without problems, however, when executing a query through the postman I get the 404 error.
The .htaccess file is the one that brings by default laravel:
<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>
and my virtualhost is the following:
<VirtualHost test-api.local:80>
ServerName test-api.local
DocumentRoot /var/www/html/restlav/public
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/test-api.local-error.log
CustomLog ${APACHE_LOG_DIR}/test-api.local-access.log combined
<Directory "/var/www/html/restlav">
Options +FollowSymLinks
RewriteEngine On
AllowOverride None
Require all granted
</Directory>
</VirtualHost>
I have already tried to make the changes that are suggested in the official documentation and nothing. The rewrite module is enabled. If anyone could help me I appreciate it in advance.
In the <Directory> section you need to set AllowOverride to All:
<VirtualHost test-api.local:80>
ServerName test-api.local
DocumentRoot /var/www/html/restlav/public
DirectoryIndex index.php
ErrorLog ${APACHE_LOG_DIR}/test-api.local-error.log
CustomLog ${APACHE_LOG_DIR}/test-api.local-access.log combined
<Directory "/var/www/html/restlav">
Options +FollowSymLinks
RewriteEngine On
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
Also make sure to enable the rewrite module in the apache server:
$ sudo a2enmod rewrite
And don't forget to finally restart the apache server to make the changes come into effect:
$ sudo systemctl restart apache2

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.

Newly installed SSL not working on apache2 - Receiving 310 Error - Too many Redirects

Disclaimer - I'm a total newb when it comes to this. Inherited this issue.
I just installed the SSL certificate to our website. Install seems to have gone fine. Whenever I try to access a page via https I receive a 310 - Too many redirects error. I suspect this has something to do with how my virtual host is setup but I don't know enough about what I am looking at.
Here is the code from the SSL virtual host
<VirtualHost *:443>
SSLEngine On
SSLCertificateFile "/path/file.crt"
SSLCertificateKeyFile "/path/file.key"
SSLCertificateChainFile "/path/file.crt"
ServerAdmin admins#email.com
ServerName domain.com
DocumentRoot /var/www/domain.com/www
<Directory /var/www/domain.com/www>
SSLRequireSSL
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^labs$ http://www.mdlabtests.com/ [L]
RewriteRule ^imaging$ http://prepaidimaging.com/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z]*)$ /index.php?aid=$1 [L]
</Directory>
ErrorLog /path/file.log
TransferLog /path/file.log
LogLevel warn
Alias /comp /var/www/domain.com/www/comp.php
<Location /card>
DirectoryIndex create_card.php
</Location>
php_admin_value session.cookie_domain "domain.com"
php_admin_flag session.cookie_secure off
php_admin_value open_basedir "/var/www/domain.com/:/var/www/sub_dir/cg/:/usr/share/php/"
php_value include_path ".:/var/www/domain.com/includes:/usr/share/php"
php_admin_flag file_uploads off
The page I am trying to access is https://domain.com/enrollment.php
Any ideas?
Or do you have an .htaccess file lurking on your filesystem?

Resources