Apache website conversion from alias to virtualhost - apache

I have to take over an internal Web server that has been configured by a colleague who is no longer there. Our developpers team asks me to convert applications URLs from an Alias to a Virtualhosts (subdomain naming) system. For instance:
https://srv-intra.mydomain.fr/basecolldev
should become
https://basecolldev.mydomain.fr
Environment specifications:
Linux OpenSUSE Leap 15.3
Server version: Apache/2.4.51 (Linux/SUSE)
PHP 7.4.6 (cli) ( NTS )
Issue description: I can connect to https://basecolldev.mydomain.fr despite some 404 errors showing in access_log file (see at the end of the post). I have then a 404 error page when clicking the login button that redirects to https://basecolldev.mydomain.fr/login. The login page is found and displayed properly when I manually modify the URL to https://basecolldev.mydomain.fr/index.php/login. This means that /index.php is not required for home page connection but is for browsing the website. I need to permanently prevent the /index.php suffix to show up in URL.
I describe below what I have configured so far and what remains unfunctionnal.
I have first created a new vhost file /etc/apache2/vhost.d/basecolldev-ssl.conf :
<VirtualHost basecolldev.mydomain.fr:443>
DocumentRoot "/var/www/BaseCollDev/public"
ServerName basecolldev.mydomain.fr
ErrorLog /var/log/apache2/basecolldev-error_log
TransferLog /var/log/apache2/basecolldev-access_log
LogLevel alert rewrite:trace8
<Directory /var/www/BaseCollDev/public>
#Order allow,deny
#allow from all
#AllowOverride All
Require all granted
Options -Indexes -Includes -ExecCGI -FollowSymlinks
</Directory>
</VirtualHost>
I have then commented the lines related to the application Directory tag in file /etc/apache2/default-server.conf :
DocumentRoot "/var/www"
<Directory "/var/www">
Options FollowSymLinks
AllowOverride None
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
Alias /icons/ "/usr/share/apache2/icons/"
Alias /phpmyadmin "/srv/www/htdocs/phpMyAdmin"
<Directory "/usr/share/apache2/icons">
Options Indexes MultiViews
AllowOverride None
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
#<Directory "/var/www/BaseCollDev/public">
# AllowOverride All
# Require all granted
#</Directory>
ScriptAlias /cgi-bin/ "/srv/www/cgi-bin/"
<Directory "/srv/www/cgi-bin">
AllowOverride None
Options +ExecCGI -Includes
<IfModule !mod_access_compat.c>
Require all granted
</IfModule>
<IfModule mod_access_compat.c>
Order allow,deny
Allow from all
</IfModule>
</Directory>
<IfModule mod_userdir.c>
UserDir public_html
Include /etc/apache2/mod_userdir.conf
</IfModule>
IncludeOptional /etc/apache2/conf.d/*.conf
IncludeOptional /etc/apache2/conf.d/apache2-manual?conf
Extract from /var/log/apache2/basecolldev-access_log file relating to a connection to the home page:
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET / HTTP/1.1" 200 54206
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/runtime.d94b3b43.js HTTP/1.1" 200 1505
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/app.13d64c6c.js HTTP/1.1" 200 236
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/2.9935185b.css HTTP/1.1" 200 64639
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/app.6cefaab5.css HTTP/1.1" 200 163752
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/2.28b18d57.js HTTP/1.1" 200 261268
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/images/logoEdVBlanc.png HTTP/1.1" 200 6579
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:31 +0200] "GET /build/fonts/fa-solid-900.e8a427e1.woff2 HTTP/1.1" 200 78196
10.9.4.140 - - [03/Jun/2022:11:02:32 +0200] "GET /favicon.ico HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:32 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:33 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:34 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
10.9.4.140 - - [03/Jun/2022:11:02:35 +0200] "GET /_wdt/b07606 HTTP/1.1" 404 1280
Extract from /var/log/apache2/basecolldev-access_log file relating to a connection to the login page:
10.9.4.140 - - [03/Jun/2022:11:03:47 +0200] "GET /login HTTP/1.1" 404 1280
I see no logs in /var/log/apache2/basecolldev-error_log file.
I enclose .htaccess files contents if necessary.
File /var/www/.htaccess :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
File /var/www/BaseCollDev/public/.htaccess :
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>

You have AllowOverride None in your <Directory "/var/www">. This means your .htaccess file and therefore your mod_rewrite configuration is not being read. The simplest option is AllowOverride All (you can specify specific overrides too if you want).
You will also need to allow following symlinks (remove -FollowSymlinks, possibly add +FollowSymlinks).

I eventually managed to make it functional, by following:
File /etc/apache2/default-server.conf > changed Allow Override to All:
<Directory "/var/www">
...
AllowOverride All
...
</Directory>
File /etc/apache2/vhosts.d/basecoll-ssl.conf > removed -FollowSymlinks option:
<VirtualHost basecolldev.mydomain.fr:443>
...
<Directory /var/www/BaseCollDev/public>
...
Options -Indexes -Includes -ExecCGI
...
</Directory>
</VirtualHost>
Thanks for help that put me on the right way!

Use AliasMatch instead of Alias:
AliasMatch ^/bar/?(.*) /var/www/bar/$1

Related

XAMPP Virtual Hosts not working on Windows 10

Before marking this as answered, please read the entire thing because yes there is a good amount of these questions but NONE of the answers on them have worked at all.
Hosts File
127.0.0.1 localhost
127.0.0.1 abv
httpd.conf
<Directory />
AllowOverride none
Require all granted
</Directory>
index.php
echo "abc";
httpd-vhosts.conf
<VirtualHost *:80>
DocumentRoot "/xampp/htdocs/abv"
ServerName abv
ErrorLog "abv-error.log"
CustomLog "abv-access.log" common
<Directory "/xampp/htdocs/abv">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Access LOG
127.0.0.1 - - [21/Jul/2019:10:09:50 +0530] "GET / HTTP/1.1" 200 3
127.0.0.1 - - [21/Jul/2019:10:09:50 +0530] "GET /favicon.ico HTTP/1.1" 404 1053
These Setting are running perfectly and output is showing and now replacing with below
<VirtualHost *:80>
DocumentRoot "F:\Websites\abv"
ServerName abv
ErrorLog "abv-error.log"
CustomLog "abv-access.log" common
<Directory "F:\Websites\abv">
DirectoryIndex index.php
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Access LOG
127.0.0.1 - - [21/Jul/2019:10:09:57 +0530] "GET / HTTP/1.1" 200 384
127.0.0.1 - - [21/Jul/2019:10:09:57 +0530] "GET /favicon.ico HTTP/1.1" 404 1053
No out is showing WHY?

Symlink to /project/current/web work but not to /project/current/web/app.php (Apache / Ubuntu / Symfony2)

Symlink to /var/www/myproject/current/web/ is working but is listing the web directory (not good).
Symlink to /var/www/myproject/current/web/app.php isn't working and give me this error : The requested URL / was not found on this server.
no logs in /var/log/apache2/error.log and this line in /var/log/apache2/access.log - 404 error
ip - - [17/Mar/2016:06:21:15 -0400] "GET / HTTP/1.1" 404 493 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"
Here are the set up :
Symfony2 + Capisfony + Apache + Ubuntu 14.04
Here is my apache2.conf
<Directory />
Options FollowSymLinks
AllowOverride all
Require all denied
</Directory>
<Directory /usr/share>
AllowOverride None
Require all granted
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Here is the .htaccess in /var/www/myproject/current/web
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
And here is how I make the sym link
rm -rf /var/www/html
ln -s /var/www/myproject/current/web/app.php /var/www/html
service apache2 restart
==> 404
rm -rf /var/www/html
ln -s /var/www/myproject/web/app.php /var/www/html
service apache2 restart
=> You don't have permission to access / on this server.
[Thu Mar 17 06:44:53.385300 2016] [core:error] [pid 19767] [client 88.14.213.213:51484] AH00037: Symbolic link not allowed or link target not accessible: /var/www/html
I suggest you to work with the vhost of apache instead :
It's from the symfony cookbook : http://symfony.com/doc/current/cookbook/configuration/web_server_configuration.html
<VirtualHost *:80>
ServerName domain.tld
ServerAlias www.domain.tld
DocumentRoot /var/www/project/web
<Directory /var/www/project/web>
AllowOverride All
Order Allow,Deny
Allow from All
</Directory>
# uncomment the following lines if you install assets as symlinks
# or run into problems when compiling LESS/Sass/CoffeScript assets
# <Directory /var/www/project>
# Options FollowSymlinks
# </Directory>
ErrorLog /var/log/apache2/project_error.log
CustomLog /var/log/apache2/project_access.log combined
</VirtualHost>
You let apache manage the document root, and you add app_dev.php to url to use the dev environment, it's convenient if you have another env, like staging, on to test the prod env sometimes
To prevent listing of files add :
Options -Indexes
to your directory section of the vhost
The answer is :
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride all
Require all granted
</Directory>
The AllowOverride None was blocking the .htaccess
Thanks for helping.

Receiving 404 error after setting up mod_rewrite

I'm developing a website locally on Ubuntu + Apache + Virtual hosts, but am having some problems customising urls with mod_rewite. First I was getting an internal server error, but then I found I had to turn mod_rewite on so I found this tutorial...
https://www.digitalocean.com/community/tutorials/how-to-set-up-mod_rewrite
...swapping out their path names for my own.
My virtual hosts sites-available conf file codecourse.dev.conf...
<VirtualHost *:80>
ServerName codecourse.dev
ServerAlias www.codecourse.dev
ServerAdmin admin#codecourse.dev
DocumentRoot /var/www/codecourse.dev/public_html
<Directory /var/www/codecourse.dev/public_html >
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
My .htaccess file which lives inside the public folder...
Options -MultiViews
RewriteEngine On
RewriteBase /var/www/codecourse.dev/public_html/public
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
My apache access log when trying to pass the url: codecourse.dev/public/home/index/Leon
192.168.0.105 - - [20/Nov/2015:08:30:32 +0200] "GET /public/home/index/Leon HTTP/1.1" 404 544 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"
Note: This access error only appears when passing a custom url. Otherwise accessing codecourse.dev/public works as expected.
Can anyone see what I'm doing wrong here?
Notice, you have set your DocumentRoot in Virtualhost, remove RewriteBase or set it to /public.
Btw, for checking yourself you can add R flag to your RewriteRule and see, what url you get.

Apache2 - Error: 403 after installation of RewriteEngine

I have to following problem. After installing RewriteEngine on my Apache server by sudo a2enmode rewrite && service-apache restart I am getting a 403 Permission Error, when trying to access the Website. The error log reads:
[Mon Aug 03 16:47:04.772033 2015] [authz_core:error] [pid 27107] [client ::1:41610] AH01630: client denied by server configuration: /home/lenxn/Venuzle/vvm/public/
The access log:
::1 - - [03/Aug/2015:17:08:11 +0200] "GET / HTTP/1.1" 403 2267 "-" "Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.8.0"
The apache2.conf remained unaffected from the installation and the .htaccess in my source root directory looks like:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteCond %{ENV:HTTPS} !=on
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
What is the problem here? I have been able to access the server previous to the installation. Have there been any files created or alternated, which overrule my previous permission settings?
I finally fixed it by altering the section
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
from the apache2.conf to
<Directory />
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory> .

Url rewrite is not working in some cases with XAMPP in Windows 7

I have a PHP website hosted at localhost on my Windows 7 PC, running XAMPP 1.8.2.
And I have rewrite rules as below:
Options -Indexes +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?rt=$1 [QSA,L]
</IfModule>
To rewrite the URL so it will always go into something like: http://mysite.com/index.php?rt=[controller]/[action]/[params]
It works prefect until I found it failed when processing url like: http://mysite.com/error/xxx. It returns a 404 status other than goes to http://mysite.com/index.php?rt=error/xxx. (I do NOT have an sub directory call error/ under my website root folder)
I carefully checked my PHP code and I am sure it is not the cause, and I upload my website to a remote server and everything is fine. So I am pretty sure something is not properly configured in my XAMPP (Apache), that makes the /error/xxx request was treated differently.
Also I found visiting http://mysite.com/error/ gives me 403 but http://mysite.com/[something_else]/ is fine.
Just in case, I paste my vhost.conf here as well:
<VirtualHost *:80>
DocumentRoot "/xampp/htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost *:80>
DocumentRoot "E:\htdocs\mysite.dev"
ServerName mysite.dev
</VirtualHost>
And related httpd.conf:
<Directory "E:\htdocs">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Require all granted
</Directory>
Any help is appreciated!
Finally figure it out after wasting another hour on it!
There is configuration file called httpd-multilang-errordoc.conf under /xampp/apache/conf/extra, and inside it's something like this:
<IfModule alias_module>
<IfModule include_module>
<IfModule negotiation_module>
Alias /error/ "C:/xampp/apache/error/"
<Directory "C:/xampp/apache/error">
AllowOverride None
Options IncludesNoExec
AddOutputFilter Includes html
AddHandler type-map var
Require all granted
LanguagePriority en cs de es fr it ja ko nl pl pt-br ro sv tr
ForceLanguagePriority Prefer Fallback
</Directory>
ErrorDocument 400 /error/HTTP_BAD_REQUEST.html.var
ErrorDocument 401 /error/HTTP_UNAUTHORIZED.html.var
ErrorDocument 403 /error/HTTP_FORBIDDEN.html.var
ErrorDocument 404 /error/HTTP_NOT_FOUND.html.var
ErrorDocument 405 /error/HTTP_METHOD_NOT_ALLOWED.html.var
ErrorDocument 408 /error/HTTP_REQUEST_TIME_OUT.html.var
ErrorDocument 410 /error/HTTP_GONE.html.var
ErrorDocument 411 /error/HTTP_LENGTH_REQUIRED.html.var
ErrorDocument 412 /error/HTTP_PRECONDITION_FAILED.html.var
ErrorDocument 413 /error/HTTP_REQUEST_ENTITY_TOO_LARGE.html.var
ErrorDocument 414 /error/HTTP_REQUEST_URI_TOO_LARGE.html.var
ErrorDocument 415 /error/HTTP_UNSUPPORTED_MEDIA_TYPE.html.var
ErrorDocument 500 /error/HTTP_INTERNAL_SERVER_ERROR.html.var
ErrorDocument 501 /error/HTTP_NOT_IMPLEMENTED.html.var
ErrorDocument 502 /error/HTTP_BAD_GATEWAY.html.var
ErrorDocument 503 /error/HTTP_SERVICE_UNAVAILABLE.html.var
ErrorDocument 506 /error/HTTP_VARIANT_ALSO_VARIES.html.var
</IfModule>
</IfModule>
</IfModule>
The line Alias /error/ "C:/xampp/apache/error/" handles all the request matches domainname.com/error/ and rewrite it to C:/xampp/apache/error/.
Simply commenting out the line solves my problem. Not sure what it does but make things done.
From your last comment it sounds like Apache isn't even correctly configured to the path with your site!
You need to edit the Xampp\apache\conf\httpd.conf file.
First make a BACKUP copy of it.
Edit the line that starts with...
DocumentRoot "
..if it is a Windows machine and it uses / instead of \ retain those.
Then you need to edit the line that starts with...
<Directory "
Be aware that there is also a CGI version of this.
If you have correctly edited the file save and then restart Apache. Changes will NOT take effect until you restart Apache.