VirtualHost Alias loads / redirects with "/myapp/" but errors without slash "/myapp" - virtualhost

I have a VirtualHost Alias path that loads / redirects with "/myapp/" but not "/myapp".
My sever info:
OS: Red Hat Enterprise Linux Server 7.6 (Maipo)
Kernel: Linux 3.10.*
My /etc/httpd/conf.d/main.conf file:
<VirtualHost *:80>
Alias /myapp /m009/www/myapp-source
<Directory /m009/www/myapp-source >
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
If I go to mydomain.com/myapp/ (note the extra slash), I get:
Here's the app!
If I go to mydomain.com/myapp (no extra slash), I get:
- takes a long time to load
- redirects to mydomain:9080/myapp
- says "site can’t be reached"

I am not sure this is the correct answer as I do not know all the information.
Are you running an app on port 9080. Could it help to put ProxyPass /myapp ! to the virtualHost?
Sorry if not.

Alias is for exactly matching a path, so you have three options:
Add a second Alias statement with a trailing slash. This could be bad because you'll have multiple paths to the same resource. e.g. Alias /myapp/ /m009/www/myapp-source
Use AliasMatch to match the path with or without a trailing slash. e.g.
AliasMatch "/myapp/?" "/m009/www/myapp-source"
[Recommended] Rewrite requests to remove a trailing slash. Beware that this could cause a loop depending on how your Apache installation is set up. Check if DirectorySlash is on or off first.
<VirtualHost *:80>
RewriteEngine on
RewriteRule ^(.*)/+$ $1 [R=301,L]
Alias /myapp /m009/www/myapp-source
<Directory /m009/www/myapp-source >
Options Indexes FollowSymlinks MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>

Related

How to add a subpath to root url on Apache

I'm using Apache 2.4 (on a Drupal 7/Docker project).
I want my all my site pages to map from /var/www/html to http://domain/foo/ (eg: /var/www/html/1/2/3/ to http://domain/foo/1/2/3/)
I prefer to avoid using .htacess and only apache conf file
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ServerName foo_name
# Try 1
Alias /foo /var/www/html
# Try 2
Redirect "/" "/foo/"
<Directory /var/www/html>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
AllowOverride None
Order Allow,Deny
Allow from All
# Try 3
RewriteEngine On
RewriteBase /foo
To do so I tried :
Alias : Alias /foo /var/www/html so I can access to my front page with http://domain/foo/ but it doesn't work on http://domain/foo/1/2/3/). And also it's just a one way redirection.
Redirect Redirect "/" "/foo/" : I did not worked on my bowser I try to acces to http://domain/foo/ but I got this url (almost infinite loop) http://domain/foo/foo/foo/foo/foo/foo/foo/foo/foo/...
RewriteBase : RewriteBase /foo but it doesn't work probably because I need to match it with htacess on subdirectory.
Most advanced rewriting : I saw a lot of them on StackOverflock/ServerFault but it did'nt seem appropriate for my problem (I never use before regex PCRE, harder to adapt). Moreover I should avoid using rewrite for that case.
It's working like that (alias + RewriteRule) :
Alias /foo /var/www/html
<Directory /var/www/html>
DirectoryIndex index.php
Options Indexes FollowSymLinks
AllowOverride All
AllowOverride None
Order Allow,Deny
Allow from All
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

DirectorySlash Off doesn't work

I'm using apache 2.4.7
This is my virtualhost's config:
DocumentRoot /var/www/login
ServerName login.mydomain.com
<Directory "/var/www/login">
allow from all
AllowOverride All
Options +Indexes
DirectoryIndex index.html index.php
DirectorySlash Off
Require all granted
</Directory>
In my /var/www/login directory, I have a directory called freeseat and in there, there's the index.php file, so it's full location is /var/www/login/freeseat/index.php
When I try to access it through the following link (domain replaced), it redirects to the same URL with a trailing slash:
http://login.mydomain.com/freeseat -> http://login.mydomain.com/freeseat/
Why isn't the DirectorySlash Off working? I tried to put it in .htaccess, but that didn't help either.
Thanks a lot for your help,
David
First of all, you vHost won't work if you don't use the directive.
This works:
<VirtualHost *:80>
DocumentRoot /var/www/login
ServerName login.mydomain.com
<Directory "/var/www/login">
Allow from all
AllowOverride All
Options +Indexes
DirectoryIndex index.html index.php
DirectorySlash Off
Require all granted
</Directory>
ErrorLog /var/www/login/error.log
</VirtualHost>
To answer you question: DirectorySlash works as expected.
Also see apache documentation for mod_dir, especially the red box called "Security Warning" of section "DirectorySlash".
Tip: always use not publicly accessible error logs to determine the root of evil. :)
Edit #1:
I think I might have misunderstood your question. You want to access /var/www/login/freeseat/index.php through http://login.mydomain.com/freeseat (w/o trailing slash).
Then, drop the freeseat folder and use index.php as var/www/login/freeseat.php and create a RewriteRule in the /var/www/login/.htaccess file (mod_rewrite must be active):
RewriteEngine On
RewriteRule ^freeseat$ freeseat.php
Also, drop
Options +Indexes
DirectoryIndex index.html index.php
from your vHost configuration. It isn't needed anymore.

Non-existing file/URL returns 403 Forbidden

Going to example.com/config or example.com/account/login returns 403 Forbidden.
In reality, /config and /account/login should be redirected to index.php but it gives 403.
My directory structure is like this:
/var/www/example
└─/assets
└─/bower_components
└─/node_modules
└─/partials
└─/templates
└─/tests
└─/vendor
└─index.php
This is my Virtual host configuration:
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example
<DirectoryMatch "^/var/www/example/(?!(assets|partials))\w+">
Require all denied
</DirectoryMatch>
<Location />
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</Location>
</VirtualHost>
I want to disallow access to every folder except assets and partials, that's why I added that DirectoryMatch directive.
I use a PHP framework called Slim so I have to add Location and Rewrite directives.
I think Apache thinks my URLs are directories and blocks them. Is there a way to unblock my URLs?
Try adding:
<Directory "/var/www/example">
Order Allow,Deny
Allow From all
</Directory>
above the <DirectoryMatch> container in your vhost config.
Since you're using apache 2.4, then use require all:
<Directory "/var/www/example">
Require all granted
</Directory>
If you're using Apache 2.4 try to add in the end of your VirtualHost:
<Directory /var/www/example>
Options Indexes FollowSymLinks MultiViews
# If you want to enable overrides, you should read:
# http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride
AllowOverride All
Require all granted
Satisfy Any
Order allow,deny
Allow from all
</Directory>

.htaccess rewrites not being read

I have the following in htaccess:
RewriteEngine on
RewriteRule ^list/([A-Za-z0-9_\.-]+).html?$ list.php?table=$1 [QSA,L]
In virtualhosts:
ServerName localhost
DocumentRoot "c:/wamp/www"
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
<VirtualHost *:80>
DocumentRoot "c:/wamp/www/kurz/site/www"
ServerName kurz.local
<Directory c:/wamp/www/kurz/site/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
</VirtualHost>
in apache config:
DocumentRoot "c:/wamp/www/"
<Directory />
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Now the weird problem is:
there is a rewrite happening, even AFTER i delete the .htaccess, I tried also removing ALL other sites in the www folder, but there's still a rewrite happening from somewhere (do these things cache? i tried clearing browser cache and all that),
BUT
if I put some jibberish in the .htaccess, i get a server error, so I know it is being read!
the other problem is that the rewrite is not passing any variables,
this link works:
http://kurz.local/admin/list/pages.html
but it seems to take to:
http://kurz.local/admin/list.php
instead of
http://kurz.local/admin/list.php?table=pages
online this setup is working, but locally it's not
any ideas ?
i'm really puzzled!
You haven't mentioned if you restarted the server? editing server config files needs a restart, but I believe .htaccess file changes do not need apache server restart.
Your server doc root is:
c:/wamp/www/
Your virtual host doc root is inside the above, I don't think that is allowed.
It should be :
c:/wamp/vhosts/site1root
c:/wamp/vhosts/site2root
etc.
Also see this: https://httpd.apache.org/docs/2.2/vhosts/name-based.html

xampp - mysite.local redirects to xampp folder

I've been battering my head against this all evening and can't see where I'm going wrong. I want to set a host, mysite.local, on xampp and have followed all the instructions, but I keep getting redirected to mysite.local/xampp.
Any ideas where I'm going wrong here? The paths are correct, and I've restarted Apache :)
I edited my hosts file to add:
127.0.0.1 mysite.local
I edited extra/httpd-vhosts.conf:
NameVirtualHost localhost:80
<VirtualHost localhost:80>
ServerName localhost
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs"
<Directory "/Applications/XAMPP/xamppfiles/htdocs">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
</VirtualHost>
<VirtualHost localhost:80>
<Directory "/Applications/XAMPP/xamppfiles/htdocs/wd">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
</Directory>
DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/wd"
ServerName mysite.local
</VirtualHost>
I've just got the very same problem yesterday. Even if the steps you did are correct in a context, you need to do some more tasks ;)
You also need to edit Apach'es httpd.conf referring to your new VirtualHost like this:
# Your great site!
<Directory "/Applications/XAMPP/xamppfiles/htdocs/wd">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
With this alone you'll be able to access http://mysite.local without the redirection to the XAMPP splash screen BUT you'll see the directories of your proyect (at least if you don't have and index in the root folder)
If you need to load a file from a folder (for example /public/index.php) you'll need to use an .htaccess file. Remember this file must be in the folder(s) you want to have control. So for example, an .htaccess file located at the root of your project to redirect to the /public/index.php file you must do it this way:
RewriteEngine On
RewriteBase /
RewriteRule ^.*$ public/index.php [NC,L]
Just remember to use the correct regular expression you need and don't forget to take preventive measures with more security in a production website ;) I wish I've helped you out =)