Removing Trailing Slash from a directory in htaccess

The following is the directory structure

Root/ index.php contact.php projects.php /index.php /project1.php /project2.php 

I have rewrites to remove the .php extension from all file names. It works great and I can access www.website.com/projects/project2.php from www.website.com/projects/project2

I also want to have access to www.website.com/projects/index.php as www.website.com/projects

I was able to write a rule that rewrites the url to www.website.com/projects/ when I type www.website.com/projects

However, I cannot get rid of the last trailing slash.

Please note that I do not really understand this. Most of this from what I found on the Internet. I looked around a lot, but still have not received anything to work.

Here is the code:

 Options +FollowSymLinks -MultiViews RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.php [NC,L] RewriteRule ^projects$ /projects/index.php [L,E=LOOP:1] 
+4
source share
1 answer

This is called mod_dir and the DirectorySlash directive . It will automatically send 301 redirect requests for a directory that does not have a trailing slash. This fixes the security problem of information disclosure (described in the link above), which lists the contents of the directory even if there is an index file (for example, index.php ). Therefore, if you disable this feature, be very careful in your directories. If you have disabled directory indexing, this is not so important.

You can rotate slashes with:

 DirectorySlash Off 

You can disable directory indexing using the options:

 Options -Indexes 

And then you need your projects to execute the rule up to your php extension rule:

 Options +FollowSymLinks -MultiViews -Indexes DirectorySlash Off RewriteEngine on RewriteRule ^projects$ /projects/index.php [L,E=LOOP:1] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^\.]+)$ $1.php [NC,L] 
+9
source

Source: https://habr.com/ru/post/1479436/


All Articles