0

I have removed index.php from URL in Codeigniter. But I can access URL with both ways like.

abc.com/index.php/123

abc.com/123

But I need if somebody type abc.com/index.php/123 it will be redirected to the abc.com/123

What I did below code i written in .htaccess

<IfModule mod_rewrite.c>

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

And in config.php i have removed index.php

$config['index_page'] = '';

But still i can open url with both ways

Nishant Saini
  • 421
  • 3
  • 13

1 Answers1

0

You can use Apache redirects inside your .htaccess file or inside your <VirtualHost *:80> block, in your case this might look like:

RedirectMatch /index.php/(.*)$ http://example.com/$1

Also, here's a nice guide on using them https://www.linode.com/docs/web-servers/apache-tips-and-tricks/redirect-urls-with-the-apache-web-server/

Nick Miles
  • 26
  • 2
  • Please don't post link-only answers. Include the actual solution into the answer and only have the link as a reference. It that link changes/gets removed, this answer won't be helpful for future visitors. – M. Eriksson Feb 22 '19 at 10:18
  • I have posted the solution on the first line, exactly what he was asking for. The guide is if he wants to learn more about how to use apache redirects or setting it up – Nick Miles Feb 22 '19 at 10:19
  • You only gave an abstract example. Please modify it to fit the OP's needs and also explain _where_ the OP should put this. If not, then the OP (or future visitors) might still need to visit the link, or google, about how to actually use it. See SO as a class room and you need to explain in detail how to solve the equations. – M. Eriksson Feb 22 '19 at 10:21
  • 1
    ok added information on where to insert the code. Thanks – Nick Miles Feb 22 '19 at 10:27
  • Can i do it with php without using htaccess – Nishant Saini Feb 22 '19 at 10:35
  • It can be done from PHP using ```header("Location: http://example.com/...");``` but I would suggest in your case where you are trying to enforce a global redirect it is far better done from apache. Just to add, your rewriterule appears to be backwards - it redirects everything to index.php rather than away from it. – Nick Miles Feb 22 '19 at 10:55
  • @NickMiles : I have done with htaccess but i can access url with both ways – Nishant Saini Feb 22 '19 at 11:12