Question

This is my .httaccess file.

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
RewriteRule ^(.*)$ mysite.com/$1 [L,R=301] << http: has been deleted to post this message.
RewriteBase /
RewriteRule ^gallery/([^/]*)$ /gallery.php?page=$1 [L,NC,QSA]
RewriteRule ^venues/([a-zA-Z0-9-/]+)/$ /venues.php?page=$1 [L,NC,QSA]

Works perfect with the previously created links,

like if the URL is:

mysite.com/gallery/my-old-link

module rewrites it to mysite.com/gallery.php?page=my-old-link

Just added a new category, nothing special, the same characters -

mysite.com/gallery/my-new-link

I was expecting module rewrites it to

mysite.com/gallery.php?page=my-new-link

But getting this:

mysite.com/gallery/my-new-link/?page=my-new-link

And a page with the 403 error message:

Forbidden

You don't have permission to access /gallery/my-new-link/ on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Was it helpful?

Solution

Since there's a directory at /gallery/my-new-link, the directory gets handled by mod_dir because it's missing the trailing slash. What mod_dir will try to do is redirect the request for a directory that's missing the trailing slash to the same request but with the trailing slash. This redirect is happening at the same time that mod_rewrite is rewriting the URI, and both get applied to the same request, resulting in a wonky redirect.

That means you'll need to turn off mod_dir, but that's kind of bad:

The DirectorySlash documentation says:

Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.

So you'll need to turn off mod_dir while also attempting to do what mod_dir does by redirecting requests for directories missing the trailing slash to the same request with the trailing slash:

DirectorySlash Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mysite.com [NC]
RewriteRule ^(.*)$ mysite.com/$1 [L,R=301] << http: has been deleted to post this message.
RewriteBase /
RewriteRule ^gallery/([^/]*)$ /gallery.php?page=$1 [L,NC,QSA]
RewriteRule ^venues/([a-zA-Z0-9-/]+)/$ /venues.php?page=$1 [L,NC,QSA]

# redirect with trailing slash for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [L,R]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top