Skip to content
Advertisement

.htaccess is causing 403 on one link only

Bit of an odd one and I feel it must be answered but I can’t seem to find it.

I have the following in my .htaccess file on a custom PHP site (NOT WORDPRESS):

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(contact|gallery|links)$ $1.php [L]

I cannot work out why “gallery” only is giving me a 403.

example.com/links
example.com/contact

Both work as expected and go to the desired pages. However

example.com/gallery

Redirects to

example.com/gallery/

And throws the 403 Forbidden error

gallery.php is a page and can be accessed from the same URL

There’s nothing on gallery.php to redirect or check a query string or compare the URI. I cannot understand it.

Really flying blind on .htaccess stuff (always have), so any help greatly appreciated. What am I doing wrong?!

Also, tested it on https://htaccess.madewithlove.com/ and it said my work was correct, so really don’t understand what it’s finding incorrect. Why does it think gallery is a subfolder, not a URL?

And I have JUST realised, I do actually have a gallery folder, so if anyone can tell me how to use the htaccess to access the page and not the folder, I’d greatly appreciate it! If it helps, there’s a .htaccess in the gallery folder to prevent direct access to images:

Options -Indexes
Options -ExecCGI 
# AddHandler cgi-script .php .php3 .php4 .phtml .pl .py .jsp .asp .htm .shtml .sh .cgi

<Files ^(*.jpeg|*.jpg)>
order deny,allow
deny from all
</Files>
<FilesMatch ".(jpe?g)$">
order allow,deny
allow from all
</FilesMatch>

Would they be conflicting?

Advertisement

Answer

Since gallery is a physical directory, you will need to set DirectorySlash Off in order to prevent mod_dir appending the trailing slash via a 301 external redirect. (This redirect gets cached persistently by the browser, so you will need to clear your browser cache before continuing.)

Note, however, that when disabling the DirectorySlash, you must also ensure that Options -Indexes is also set to prevent mod_autoindex from generating directory listings when the trailing slash is omitted from a directory, since a DirectoryIndex document in that directory will no longer prevent the directory listing being generated. See the security warning under the DirectorySlash directive in the Apache Docs.

You may also need to set RewriteOptions AllowNoSlash to allow mod_rewrite to match directories that have no trailing slash.

You then need to modify your existing rule…

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(contact|gallery|links)$ $1.php [L]

Remove the first two RewriteCond directives. They aren’t required since you are wanting to rewrite 3 specific URLs. If any of the conditions fail then the rewrite does not occur.

If contact, gallery or links happen to exist as physical files (very unlikely) or directories (gallery is a directory it seems) then the rewrite does not occur.


Summary

DirectorySlash Off

Options +FollowSymlinks -Indexes

RewriteEngine On
RewriteOptions AllowNoSlash

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(contact|gallery|links)$ $1.php [L]
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement