Mobile app version of vmapp.org
Login or Join
Sue5673885

: URL without trailing slash makes Apache look in non-SSL DocumentRoot I have configured a virtual host in Apache for serving content over SSL/TLS. Since I also want to allow other sites on my

@Sue5673885

Posted in: #Apache #Https #Redirects

I have configured a virtual host in Apache for serving content over SSL/TLS. Since I also want to allow other sites on my domain to use regular http, I have two document roots, one at /home/www/html and one at /home/www/html-ssl. I force SSL on my site with the following directives in a file in /etc/httpd/conf.d:

<Directory /home/www/html-ssl/foo>
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire ( %{HTTP_HOST} eq "mydomain.com" )
ErrorDocument 403 mydomain.com/foo </Directory>


This redirects any requests for mydomain.com/foo/ to mydomain.com/foo/. However, when I try to access mydomain.com/foo without the trailing slash, Apache searches for foo in /home/www/html instead of in /home/www/html-ssl, causing a 404. Is there a way to tell Apache that mydomain.com/foo should really look for the directory foo in /home/www/html-ssl, and then redirect to the https site? I thought that since DirectorySlash is on, Apache would be able to figure out that foo is a directory, at which point it would see my <Directory> section above and work its magic.

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Sue5673885

3 Comments

Sorted by latest first Latest Oldest Best

 

@Si4351233

And yet another, simpler solution. Instead of using mod_rewrite, just create an alias mapping /foo to the correct directory:

Alias /foo /home/www/html-ssl/foo
<Directory /home/www/html-ssl/foo>
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire ( %{HTTP_HOST} eq "mydomain.com" )
ErrorDocument 403 mydomain.com/foo </Directory>

10% popularity Vote Up Vote Down


 

@Radia820

An alternative fix would be to enforce the trailing slash on the end of your URLS - this would have the benefit of preventing duplicates if your not using Rel Canonical.


SOURCE

This will redirect all requests without a tailing / to the URL with
the slash on the end. (note within the 2nd part of the bracket is
those file extensions to ignore.. Since it wouldn't make sense to
enforce / on a picture URL.

RewriteCond %{REQUEST_URI} !.(php|html?|jpg|gif)$

RewriteRule ^(.*)([^/])$ %{HTTP_HOST}// [L,R=301]

10% popularity Vote Up Vote Down


 

@Sent6035632

Answering my own question. I'm not sure if this is the best solution, but I figured out how to do this using a combination of mod_rewrite and the <Directory> section I already had. Basically, any requests coming in on ports other than 443 were going to my non-SSL virtual host (duh), so Apache was mapping the URLs to files in the non-SSL document root (/home/www/html). If I rewrite all non-https requests for foo to https using mod_rewrite, the new requests will use port 443 and Apache can resolve to the correct directory:

RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/foo [NC]
RewriteCond %{HTTP_HOST} ^mydomain.com?
RewriteRule ^(.*)$ %{HTTP_HOST}%{REQUEST_URI} [R=301,L]

<Directory /home/www/html-ssl/foo>
SSLOptions +StrictRequire
SSLRequireSSL
SSLRequire ( %{HTTP_HOST} eq "mydomain.com" )
ErrorDocument 403 mydomain.com/foo </Directory>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme