0

i have a website with url

http://mywebsite.com/abc.php

i added code for removing the .php extension from url like below

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

if someone search with url

http://mywebsite.com/abc its working fine.

If anyone search with http://mywebsite.com/abc/ i need to forcefully redirect it to http://mywebsite.com/abc using htaccess

AGK
  • 86
  • 1
  • 13

2 Answers2

0

# remove trailing slash

RewriteEngine On
RewriteBase /
RewriteRule ^(.*)/$ /$1 [L,R=301]
Prince
  • 176
  • 8
  • 2
    While this code snippet may answer the question, please add some explanation as to why it does. This will make for better answers and help future users understand how the problem was solved. – Andrew Mar 15 '19 at 09:36
  • try [L,R=301] instead of [L,R] – Prince Mar 15 '19 at 10:42
  • If i tried to open http://mywebsite.com/abc/ as per the updated code it redirect to http://mywebsite.com apart from http://mywebsite.com/abc – AGK Mar 18 '19 at 09:49
  • https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url – Prince Mar 18 '19 at 12:21
0

You can use this :

RewriteEngine on

#redirect and remove trailing slash
#from all URIs except directories "!-d"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/?$ /$1 [L,R=301]
#remove .php extension
#this allows you to access php files without extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)/?$ /$1.php [QSA,L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Sorry but you need to be more specific about what exactly is not working and what error do you get. I have tested this on my webserver and it works for me using your url examples. – Amit Verma Mar 18 '19 at 09:20