What is the Apache equivalent of nginx's try_files directive - apache

Please help, i'm new to server configuration.
I'm trying to write the equivalent of the nginx server configuration below in Apache's virtual host configuration.
server {
root /path/to/public/root/directory;
server_name myservername.test;
location /api {
try_files $uri $uri/ /index.php?$query_string;
}
location /web {
try_files $uri $uri/ /index.php?$query_string;
}
location / {
proxy_pass http://localhost:80;
}
}
From what i have researched, I tried to do the following
<VirtualHost *:80>
ServerName myservername.test
DocumentRoot "/path/to/public/root/directory"
<Directory /path/to/public/root/directory/>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} "=/web" [OR]
RewriteCond %{REQUEST_FILENAME} "=/api"
RewriteRule . index.php [QSA, L]
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName myservername.test
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
</VirtualHost>
but it did not work. I have tweaked the code from top to bottom but my apache server keeps throwing errors.
My Apache version is 2.4.52
Note: It is neccessary i do this in apache's vhost and not in the .htaccess file
Please help. thanks in advance.

I finally got it to work.
The first mistake i made was forgetting to enable mod_rewrite, mod_proxy and mod_proxy_http.
second mistake was i created two vhost with the same ServerName value, apache seems to ignore one of them
After enabling those apache modules, here is the final result that worked for me
<VirtualHost *:80>
ServerName myservername.test
#
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond "%{REQUEST_URI}" "=/web" [OR]
RewriteCond "%{REQUEST_URI}" "=/api"
RewriteRule . index.php [QSA]
#
ProxyPass "/" "http://localhost:80/"
ProxyPassReverse "/" "http://localhost:80/"
</VirtualHost>

Related

How to set up my virtualhost on apache to render correct links to css and images

I've installed Scriptcase manually to my apache webserver running ubuntu 20.04 and PHP 7.3 and it is loading the page. BUT all sublinks are not working as images/css files are not rendered correctly (it can't find them)
[Image showing the page with broken links][1]
[1]: https://i.stack.imgur.com/8VodU.png
Example:
Image links are read like ... https://devel/conf/scriptcase/img/btnnew/crystal/sc_ico.png
It should be: https://wmqms-dev.wismatix.net/devel/conf/scriptcase/img/btnnew/crystal/sc_ico.png
My virtualhost file is like the below:
<VirtualHost *:80>
ServerName wmqms-dev.wismatix.net
VirtualDocumentRoot /var/www/wmqms-dev
Redirect / https://wmqms-dev.wismatix.net
# RewriteEngine on
# RewriteCond %{SERVER_NAME} =wismatix.net [OR]
# RewriteCond %{SERVER_NAME} =wmqms-dev.wismatix.net
# RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
UseCanonicalName Off
<VirtualHost *:443>
ServerName wmqms-dev.wismatix.net
VirtualDocumentRoot /var/www/wmqms-dev
<Directory /var/www/wmqms-dev/>
Options FollowSymLinks
AllowOverride None
</Directory>
# <IfModule mod_rewrite.c>
# RewriteEngine On
# RewriteBase /
# RewriteCond %{HTTP_HOST} .
# RewriteRule ^/([0-9]+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
# RewriteRule ^index\.php$ - [L]
# RewriteCond %{REQUEST_FILENAME} !-f
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]
# </IfModule>
SSLEngine On
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/wmqms-test.wismatix.net-0001/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/wmqms-test.wismatix.net-0001/privkey.pem
</VirtualHost>
I've been fiddling with this for a while and tried searching thru the internet including 'StackOverflow' and read several articles but either I don't understand them ;-) or ?
Can anyone point me in the right direction ... I'm guessing ReWrite or ?
Thanks in advance ...
Jonatan

mod_rewrite causing extra slash

I'm trying to simply remove .php from the filenames on URLs. I'm implementing the solution from this StackOverflow answer so my VirtualHost looks like this,
# domain: example.com
# public: /var/www/html/example.com/public_html/
<Directory /var/www/html/example.com/public_html>
Options +FollowSymLinks
AllowOverride All
DirectoryIndex index.html index.htm index.php
</Directory>
<VirtualHost *:80>
ServerName example.com
Redirect / https://example.com/
</VirtualHost>
<VirtualHost *:443>
# Admin details
ServerAdmin vanguard#example.com
ServerName example.com
ServerAlias www.example.com
SSLEngine on
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
#Index File
DirectoryIndex index.html index.php
DocumentRoot /var/www/html/example.com/public_html
#Log Details
LogLevel warn
ErrorLog /var/www/html/example.com/log/error.log
CustomLog /var/www/html/example.com/log/access.log combined
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
</VirtualHost>
Going to https://example.com resolves just fine, but if I try to go to https://example.com/examplefile it rewrites it to https://example.com//examplefile/
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L]
In a virtual host context this RewriteCond directive will never match because at the time the directive is processed the request has not yet been mapped to the file system, so REQUEST_FILENAME simply contains the URL-path (as opposed to the absolute filesystem path), which is the same as REQUEST_URI.
(This wouldn't work in .htaccess either, but for a different reason. In .htaccess you can't rewrite to an absolute filesystem path, so whilst the RewriteCond should match, the RewriteRule is likely to trigger a 403 Forbidden error.)
To fix this in a server/virtual host context, you can modify these directives to use a URL-based look-ahead:
RewriteCond %{LA-U:REQUEST_FILENAME}.php -f
RewriteRule !\.php$ %{LA-U:REQUEST_FILENAME}.php [L]
However, neither your original directives nor this correction explains the double slash you are seeing in your example. Either an erroneously cached response or your web application?

.htaccess multiple domains

I have one host for two domain. The first domain and its rules (aaaaa.com) are working.
When I try to append rules of second one (bbbbb.com) send an Internal Error 500 message.
The goal is bbbbb.com should reply the content of bbbbb directory. Please help..
Options -Indexes
<IfModule mod_rewrite.c>
ErrorDocument 404 /errors/error404.html
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^((?!www\.)(?!aaaaa\.com)[^\.]+)\.
RewriteRule ^(.*)$ aaaaa/index.html?p=%1 [L]
RewriteCond %{HTTP_HOST} ^bbbbb\.com [NC]
RewriteRule ^(.*)$ bbbbb/%1 [R,L]
</IfModule>
If you have access to the apache configuration, I recommend you to use Virtual Hosts to achieve your purpose.
You should include this configuration in your /etc/apache2.conf file:
<VirtualHost *:80>
DocumentRoot "/www/aaaaa"
ServerName aaaaa.com
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "/www/bbbbb"
ServerName bbbbb.com
</VirtualHost>
Supposing that your web directories are stored in /www.
You can find the documentation here:
https://httpd.apache.org/docs/current/vhosts/

apache redirect http to https for some page

I used this code but error appear
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/user/login
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
thanks
website
"Using mod_rewrite to do this isn't the recommended behavior. See RedirectSSL"
Should to use Virtual Host instead... you can try this:
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / https://secure.example.com/
</VirtualHost>
<VirtualHost _default_:443>
ServerName secure.example.com
DocumentRoot /usr/local/apache2/htdocs
SSLEngine On
# etc...
</VirtualHost>
With mod_rewrite will should be like this:
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]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

Access to phpmyadmin is not possible now due to redirection of virtualhosts

I have a server with two subdomains, so I set up redirection for both of my subdomains specially because I want to force that the two subdomains are accessed only by https instead of http. My ideal scenario would be that I have those redirection untouched and be able to access phpMyAdmin locally (for security reasons obviously) but the problem now is that when I type the following in my browser:
127.0.0.1/phpmyadmin or localhost/phpmyadmin it takes me to https://example1.com/webservice/myrestfile-REST.php?appconfig=example
Is there any way to solve this problem?
This is the configuration that I currently have with the redirection:
<VirtualHost *:80>
ServerName example1.com
ServerAlias API.com
ErrorLog /var/www/html/error.log
CustomLog /var/www/html/requests.log combined
DocumentRoot /var/www/html
RewriteEngine On
LogLevel alert rewrite:trace6
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^ https://example1.com/webservice/myrestfile-REST.php?appconfig=example [R,L]
Redirect permanent / https://example1.com
# RewriteRule ^(.*)$ /webservice/myrestfile-REST.php?appconfig=example [QSA,L]
</VirtualHost>
<VirtualHost *:443>
ServerName example1.com
DocumentRoot /var/www/html
RewriteEngine On
RewriteRule ^(.*)$ /webservice/myrestfile-REST.php?appconfig=example [QSA,L]
SSLEngine On
SSLCertificateFile /etc/httpd/ssl/40d5d69ae6a53.crt
SSLCertificateKeyFile /etc/httpd/ssl/sitekey.key
SSLCertificateChainFile /etc/httpd/ssl/gd_bundle-g2-g1.crt
</VirtualHost>
It looks like you have apache set to listen on any ip <VirtualHost *:80> which would include the loopback address 127.0.0.1. See if you can just use the condition to ignore those.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/phpmyadmin$ [NC]
RewriteCond %{HTTP_HOST} !^127\.0\.0\.1 [OR]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule ^(.*)$ /webservice/myrestfile-REST.php?appconfig=example [QSA,L]
See how that works and let me know.