Hiptop
Hiptop

Reputation: 79

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?

Upvotes: 1

Views: 5680

Answers (5)

anubhava
anubhava

Reputation: 785146

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]

Upvotes: 2

Vishal Lunagariya
Vishal Lunagariya

Reputation: 50

if Use CodeIgniter

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

Else

    <IfModule mod_rewrite.c>
    RewriteEngine On

    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA,L]
</IfModule>

This is the basic rule to hide index.php from the URL. Put this in your root .htaccess file.

mod_rewrite must be enabled with PHP and this will work for the PHP version higher than 5.2.6.

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

RewriteEngine On RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Upvotes: 0

Rahul kalal
Rahul kalal

Reputation: 51

RewriteEngine on
RewriteBase /Projectname
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

Upvotes: 0

GrigorAtaryan
GrigorAtaryan

Reputation: 473

In your project root please create or put .htaccess file. And add following lines.

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

Upvotes: 1

Mani Nagarajan
Mani Nagarajan

Reputation: 52

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

Upvotes: 0

Related Questions