1

Removing trailing slash at the end of file names in a folder

http://www.example.com/directory/index.php/ should return to http://www.example.com/directory/index.php (that is without trailing slash at the end - it can be any file type like html, php, asp )

I tried with RewriteRule ^(.*)/$ $1 [R=301,L] , but when some one type the url with slash at the end it redirects to http://www.example.com/file which should be returned as http://www.example.com/directory/index.php

Help Appreciated.

AMLU
  • 19
  • 1
  • Unfortunately, you will never be able to handle every kind of bad typing. You can handle the very case with something like ^(.*?\.[a-z]{3,4})/$ $1 [R=301, L] but this would be just scratching the surface. – fab2s May 20 '18 at 15:03

1 Answers1

1

Try using this in your "web.config". It should be a "redirect" action not a "rewrite" as follows (It worked well for me):

            <rule name="Remove trailing slash" stopProcessing="true">  
                <match url="(.*)/$" />  
                <conditions>  
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />  
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />  
                </conditions>  
                <action type="Redirect" redirectType="Permanent" url="{R:1}" />  
            </rule>

Hope this help you too.

Code Reaper
  • 143
  • 1
  • 9
  • This fixes single occurrences. I have a case where google is seeing "example.php/example2.php. How do I remove everything after the trailing slash? – Jace Dec 03 '18 at 17:37