Emre
Emre

Reputation: 899

How to redirect requests with .php (extension)

I've found a partial answer here.

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php - [F,L]

[works] www.example.com/file.php to redirect to 404

[works] www.example.com/file to serve under /file.php (without showing on address bar)

[works] www.example.com/folder/ to NOT redirect to 404

[does not work] www.example.com/folder/index.php to redirect to 404

I'm aware that www.example.com/folder/ is served as www.example.com/folder/index.php (without showing on address bar) because of DirectoryIndex

I've tried playing around and disabling DirectoryIndex but I just couldn't figure it out.

What I have so far:

# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s?] [NC]
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/+index\.php(/[^\s\?]+)? [NC]
RewriteCond %{REQUEST_URI} !(.*)/index\.php$
RewriteRule ^(.*)$ 404.php [L]

# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

Upvotes: 0

Views: 123

Answers (2)

anubhava
anubhava

Reputation: 784918

Try this code:

# if file has a .php extension redirect to 404 page #
RewriteCond %{THE_REQUEST} \s.+?\.php[\s?] [NC]
RewriteCond %{REQUEST_URI} !/index\.php [NC]
RewriteRule ^ 404.php [L]

# this makes files work without .php #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L,QSA]

Upvotes: 0

user123456789
user123456789

Reputation: 566

Try this,

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} !-f
   RewriteCond %{REQUEST_FILENAME} !-d
   RewriteRule ^(.*)$ /yourfolder/index.php?/$1 [L]
</IfModule>

Upvotes: 1

Related Questions