Magnar Myrtveit
Magnar Myrtveit

Reputation: 2740

Avoiding trailing slash in url pointing to folder

I want to rewrite the url /login to /login/login.php. I have created a .htaccess file in /login with the rule RewriteRule ^$ login.php [L,NC,QSA]. This works great, but the url in the browser becomes /login/ (notice the trailing slash, which I don't want).

I figured the reason for this is that the folder /login is opened to find the .htaccess file, and when inside a folder a trailing slash is added(?)

To avoid this, I created a .htaccess file in / with the rule RewriteRule ^login$ login/login.php [L,NC,QSA] instead. This does not work. The url in the browser still becomes /login/, and I get a 403 Forbidden: "You don't have permission to access /login/ on this server."

Next I changed the rule (the .htaccess file is still in /) to RewriteRule ^login login/login.php [L,NC,QSA]. Now it works again, but the url in the browser still becomes /login/.

How can I remove the trailing slash (or prevent it from appearing) when the url is pointing to a folder?

Upvotes: 1

Views: 637

Answers (1)

Jon Lin
Jon Lin

Reputation: 143876

Since login is a directory, there's a module called "mod_dir" that kicks in when a request is made for a directory that is missing the trailing slash. It will automatically redirect the request to include the trailing slash and it will do this before mod_rewrite even gets to process the request.

You can turn this off by using the DirectorySlash Off directive but there's a serious information disclosure issue when you turn this off that allows people to view directory contents even if there is a default index. You can get around this by turning indexes off, but that will just return 403 forbidden.

So in the htaccess file in your document root, you can try adding:

Options -Indexes
DirecrtorySlash Off

RewriteEngine On
RewriteRule ^login$ login/login.php [L,NC,QSA]

Upvotes: 1

Related Questions