.htaccess, proper rewriting of directory and file with same name

There are two things going “wrong” here, and two ways to fix it.

The first is that apache “figures out” that there is a directory by the name of “portfolio” before the rewrite conditions are applied. That means that the rewrite conditions are receiving “portfolio/” instead of “portfolio”.

Second, the “!-d” rule is specifically avoiding the rewrite that you want to make if there is in fact a directory by that name

Solution 1: Manually re-route requests for the portfolio directory to remove the slash.

# Manually re-route portfolio/ requests to portfolio
RewriteCond %{REQUEST_FILENAME} portfolio/$
RewriteRule ^(.*)/$ $1 

# Hide extension
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Note the removal of the “!-d” condition.

The downside to this is that you are having to hard-code the “portfolio” edge case directly into the rewrite rules, and will still result in the browser being first redirected to portfolio/

Solution 2: Set DirectorySlash Off and remove directory exists test

# Disable Automatic Directory detection
DirectorySlash Off

# Hide extension
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Setting DirectorySlash Off would fix this issue the best, but may break other parts of your site where you actually want the auto DirectorySlash. Best of Luck, and I hope this helps.

Note when testing solution 2, your browser may remember the redirect of “portfolio” to “portfolio/” and perform the redirect before it even sends the request to the server. Be sure to test in a cache-clear, clean environment for best results.

Leave a Comment