3

I am working according to this question (Redirect .php urls to urls without extension) but this sends me to root folder.

RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]

for eg. my orginal url http://www.website.com/products/abc.php
after redirect it sends me to http://www.website.com/abc

Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
Saurabh Sharma
  • 430
  • 2
  • 11

1 Answers1

1

Here is a generic rule that can handle subfolders

Options -MultiViews
RewriteEngine On

# browser requests PHP
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+/)?([^/\s\?&]+)\.php
RewriteRule ^ /%1%2 [L,R=301]

# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ /$1.php [L]
Justin Iurman
  • 18,954
  • 3
  • 35
  • 54
  • thanks but i am getting 404 Error, even file exsits on system.. in other words i am getting http://www.website.com/products/abc but with 404 error and abc.php exists on server – Saurabh Sharma Jul 11 '18 at 05:53
  • @SaurabhSharma Please make sure that the file `abc.php` is located inside the folder `products` (and that they both *physically* exist, obviously). It works as expected on my side, just tested it to make sure – Justin Iurman Jul 12 '18 at 13:56