3

I'm using below htaccess to remove index.php from my url in Codeigniter.

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

The problem is that user can still use below code to access the method :

http://example.com/index.php/controller/method

How can I redirect them to url without index.php or send them to an error page if they add index.php ?

user181892
  • 53
  • 1
  • 5
  • The duplicate marking of this question is incorrect. This is a follow-on case where the questioner wants to both acheive the removal of the index.php and also redirect any existing links/bookmarks to the new URL. The related question doesn't tell you how to do that second part - which is what the questioner is clearly asking about. – scipilot Oct 25 '15 at 05:34

1 Answers1

9
RewriteEngine On

RewriteRule ^index.php/(.*)$ /$1 [R=302,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

first rule redirects user non index.php site perminantly then it comes to your second rule and bingo.

rcpayan
  • 535
  • 3
  • 15
  • Changed 301 to 302 to conserve SEO link matches within search engines. I'll delete my answer, yours has the redirect. – Sébastien Renauld May 24 '13 at 14:54
  • Now I'm getting error that this page has a redirect loop – user181892 May 24 '13 at 15:01
  • 2
    @rcpayan: change your last rule to `RewriteRule ^(.*)$ index.php?/$1 [L]`. Apache treats index.php/$1 as a redirect. – Sébastien Renauld May 24 '13 at 15:09
  • @user181892: this last comment should fix the loop :-) – Sébastien Renauld May 24 '13 at 15:09
  • @rcpayan thank you for your answer, unfortunately I can't vote up cuz I don't have enough reputation. – user181892 May 24 '13 at 15:14
  • Sebastian thx for correction, you got a point. And u dont need to vote up but you can accept it as an answer – rcpayan May 24 '13 at 17:17
  • @SébastienRenauld thank you. I never would have figured that out. Can you provide a reference for why Apache treats it so, and how the `?` is interpreted to alter that? (I can't see anything under http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule to explain it!) – scipilot Oct 25 '15 at 05:41