rewrite http folder requests
Sometimes when You type an internet address, You don't supply a trailing slash to get the contents of a folder. Thus when actually wanting the file www.host.com/folder/index.html, You just type host.com/folder and hit enter. Up to now, my Apache had a configuration which enabled it to decide in a sort of intelligent way wether the request was a folder or a file.
But after moving to the new server, this property wasn't given anymore. To provide the user still with a similar functionality without need for root access, I figured a http rewrite command which does this:
RewriteRule ^(([^\.])+[^/])$ /$1/ [R]
What it does: any request (the server name is not included within the request), which doesn't contains a dot (".") and which doesn't end with a slash ("/") will be added a slash.
This looks pretty puzzling, and it took me a moment to figure it out. Let's analyse the different parts of it:
\. | the character "." (without the backslash, "." a special operator) |
^\. | any character except "." |
[^\.] | class of all characters except "." |
([^\.])* | repetition 0 or more times of any character except "." |
/ | the character "/" |
^/ | any character except "/" |
[^/] | class of all characters except "/" |
([^\.])*[^/] | repetition 0 or more times of any character except "." concatenated with any character except "/" |
(([^\.])*[^/]) | above expression grouped for backreference |
^ ... $ | delimiters to show beginning and end of the string |
/$1/ | redirect the request to the current host (/), to the path stored in the first backreference ($1), and add a trailing slash (/) |
[R] | stands for "rewrite", meaning that it will show in the address bar of the browser |
This line is saved in a .htaccess file in the root directory of my server, after a line which says to enable the rewrite mechanism (RewriteEngine On).
Ain't that hard, after all, no?



