Search code examples
php.htaccess

Remove index.php using htaccess


I want to remove index.php from url using htaccess. Code Example:

Options +FollowSymlinks
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

For example : If my url https://url.com/index.php then how to make it https://url.com ?.

And the 2nd question is, if someone types https://url.com/directory which is contained non-index, then how to redirect them to the main domain without index.php ?

I am using simple single index file at root not a framework. and using cloudflare dns.

When i replaced last line with RewriteRule ^ %1 [R=301,L] then working.

For example : https://url.com/demo or https://url.com/demo/demo1 (not exists directory) then it redirect to https://url.com without index.php.

But when the url is https://url.com/index.php , it still showing the same url https://url.com/index.php . How to remove index.php?


Solution

  • You may use these rules in site root .htaccess:

    Options +FollowSymlinks -Indexes
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^ %1 [L,R=301,NE]
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
    RewriteRule ^ %1 [L,R=301,NE]