2

I have the following code in my htaccess file to remove the PHP extensions:

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

However, now I added a file in /directory/index.php and I can only access it using /directory/index instead of using just /directory/ (which now throws a 404 not found).

1
  • your RewriteCond %{REQUEST_FILENAME} !-f checks to see if a file is requested and also exists, so unless a file is requested and exists your rule is never going to match anything. Commented Jul 29, 2014 at 4:32

1 Answer 1

1

Keep your rules like this in your root .htaccess:

DirectoryIndex index.php

RewriteEngine On
RewriteBase /

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

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Sign up to request clarification or add additional context in comments.

3 Comments

What if I not I am doing a pagination and want to use the following structure: domain.com/directory/example has the first page of results while the pagination will be like domain.com/directory/example/page/2 Should I put those rules in a new htaccess under the directory that hosts the example.php file? Thanks!
Actually this rule has had nothing to do with pagination. It is meant to hide .php extension as per your question. Please post a new question with pagination requirements and I will try to answer it accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.