I have the following rewrite rules:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Route all URLs to dispatch.php.
RewriteCond %{REQUEST_URI} !media/
RewriteRule ^(.*)$ dispatch.php [L]
#Route requests to /media/* to /project/media/*
RewriteRule ^media/(.*)?$ project/media/$1 [L]
</IfModule>
Everything is rewritten to dispatch.php
unless the URI starts with media/
in which case it will rewrite the URI to project/media/*
. Everything works fine and if I navigate to example.com/media/css/style.css
the stylesheet it served. If I navigate to example.com/media/css/
then a 403 Forbidden error is sent. Perfect!
However, If I navigate to example.com/media/css
(missing the trailing '/') then the URL in the location bar is rewritten to example.com/project/media/css
and the request is handled by dispatch.php
. How do I stop this behaviour from happening? I would like it to be handled by dispatch.php
but without projects/
being added to the URL.
Solved: The problem was due to mod_dir redirecting the URLs that had no trailing slash. This resulted in the location as it appears on disk being used for the redirect which means that the subfolder (project) was appended to the URI. The final .htaccess that I used is as follows:
Options +FollowSymLinks +MultiViews -Indexes
Options -Indexes
<IfModule mod_dir.c>
DirectorySlash Off
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#Add trailing slashes
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1/ [R=301,L]
#Route requests to /media/* to /project/media/*
RewriteRule ^media(/.*)$ project/media$1 [L]
# Route all URLs to dispatch.php.
RewriteCond %{REQUEST_URI} !^media/(.*)
RewriteRule ^(.*)$ dispatch.php [L]
</IfModule>
The new .htaccess does the same as before as well as:
- Disables mod_dir.
- Adds trailing slashes to the URI through the use of a RewriteRule.