I am building an ajax-driven website that uses html5 pushState() to preserve its url structure and facilitate reliable page reloads, etc. For this,I have set up my htaccess in such a way that any directory path that begins with an uppercase letter and has no trailing slash is redirected to some
"domain.com/index.php?page=Directorypath"
So right now my requests are translated as:
domain.com/Folder => domain.com/index.php?page=Folder
domain.com/Folder/ , domain.com/folder, domain.com/folder/ => (default, no rewrite)
This is done by my .htaccess file (located at domain root) and has the code :
# Enable Rewriting
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^((/?[A-Z][A-Za-z0-9]*)+[^/])$
RewriteRule ^((/?[A-Z][A-Za-z0-9]*)+[^/])$ index.php?page=$1
This worked fine until I added a blank directory named "Folder" and visited "domain.com/Folder" on my browser.
Previously, "domain.com/Folder" was giving me the expected content ("domain.com/index.php?page=Folder") and "domain.com/Folder/" was giving me a 404 error, as expected since there was no directory named "Folder" in the document root.
Now my expected result was:
Browser shows "domain.com/Folder" in the url bar and server delivers the content of "domain.com/index.php?page=Folder" (same as the case when "Folder" did not exist)
But instead:
Browser shows "domain.com/Folder?page=Folder"
Can someone point out what I am doing wrong over here? And, if possible, how I could correct it?
*I have no other htaccess files in any other directory.
Edit: the scheme seems to be working fine when I force a permanent redirect [R=301], but unfortunately that changes the url in browser's address bar.
Finally, I figured it out!
The headache was caused by apache's built in DirectorySlash configuation, which was adding the trailing slash silently if the directory existed.
So I fixed it by turning it off. But when I thought about it more, I felt that it is actually a useful redirection, so I hard coded my htaccess to add trailing slashes for all other directories that doesn't start with an uppercase letter
So here is my final htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteBase /
RewriteCond %{REQUEST_URI} ^((/?[A-Z][A-Za-z0-9]*)+[^/])$
RewriteRule ^((/?[A-Z][A-Za-z0-9]*)+[^/])$ index.php?page=$1 [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([a-z0-9][A-Za-z0-9/]*[^/])$ $1/ [R,L]
Hope this might help others with similar troubles