-1

I achieved to remove php extension by using htaccess. My codes are

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]


www.abc.com/folder1/page.php -> www.abc.com/folder1/page

Of course that code removed php extension for index.php

www.abc.com/folder1/index.php -> www.abc.com/folder1/index

My question is how can I remove the word index

www.abc.com/folder1/index -> www.abc.com/folder1/

Briefly, when I type www.abc.com/folder1/page, page.php should run in folder1 and when I type www.abc.com/folder1/ index.php should run in folder1.

u238
  • 373
  • 4
  • 14
  • Try this: https://stackoverflow.com/a/4365161/3241041 – alxlives Aug 27 '19 at 18:22
  • It removed index.php but other pages have error 404 – u238 Aug 27 '19 at 19:39
  • Possible duplicate of [htaccess remove index.php from url](https://stackoverflow.com/questions/4365129/htaccess-remove-index-php-from-url) – Damien Flament Aug 27 '19 at 20:47
  • Nope, not same. It just removes index.php. I want to remove php extension for all pages and remove index. – u238 Aug 27 '19 at 21:22
  • _“and when I type www.abc.com/folder1/ index.php should run in folder1”_ - that should happen automatically, if you specify `DirectoryIndex` accordingly … – misorude Aug 28 '19 at 07:00

1 Answers1

1

You may replace your shown rule with these 2 rules in site root .htaccess:

RewriteEngine On

# To externally redirect /dir/file.php to /dir/file and remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index|(\S+?))\.php[/\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Unfortunately it didn't work. It removde index (www.abc.com/folder1/index.php turns into www.abc.com/folder1/) but other pages have error 404 (www.abc.com/folder1/page.php should've turned into www.abc.com/folder1/page but got error 404) – u238 Aug 27 '19 at 21:41
  • I just copied and pasted your code. On my localhost http://localhost/example/index.php is redirected to http://localhost/example/ it's OK but when I type http://localhost/example/folder1/page it's not redirected to http://localhost/example/folder1/page.php I got error 404 Object not found. – u238 Aug 27 '19 at 22:21