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):

JavaScript

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

JavaScript

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

JavaScript

Redirects to

JavaScript

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:

JavaScript

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…

JavaScript

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

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement