Victor Bocharsky
Victor Bocharsky

Reputation: 12306

How to remove index.php with .htaccess

I used next .htaccess code:

RewriteEngine on
# if the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^(.*)$ index.php/$1 [L]

It work nice, but it's available all pages with index.php and without it, for example:

http://localhost/framework/about
http://localhost/framework/index.php/about // need redirect to http://localhost/framework/about

I want to redirect all request with index.php to without index.php with SEO reasons, How can I remove index.php?

Upvotes: 1

Views: 112

Answers (2)

Justin Iurman
Justin Iurman

Reputation: 19026

Try this

RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^/]+)/index\.php/(.+)\s [NC]
RewriteRule ^.*$ /%1/%2 [R=301,L]

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


  1. %{THE_REQUEST} contains the full HTTP request line sent by the browser to the server (e.g., GET /index.html HTTP/1.1)
  2. The condition RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/([^/]+)/index\.php/(.+)\s [NC] matches something like GET /xxx/index.php/yyyy where xxx would be framework and yyy would be about/something/what/you/want or just about if you want (could be also POST or another instead of GET but it does not really matter here).

Example: /framework/index.php/everything/about/me will be redirected to /framework/everything/about/me or /framework/index.php/about will be redirected to /framework/about

Upvotes: 2

Tigger
Tigger

Reputation: 9130

Pretty sure the following will redirect everything to the $_SERVER['PATH_INFO'] var in index.php

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !.index.ph.*
RewriteRule ^(.*)$  /index.php

It has been a while since I've used this method.

Alternatively you can use ForceType like this:

<Files *>
    ForceType application/x-httpd-php
</Files>
<Files *\.*>
    ForceType None
</Files>

This will allow you to have a PHP file called about (no extension) and have it executed as PHP.

Upvotes: 1

Related Questions