6

Is it possible that ci url will not work with index.php.I dont want to add index.php in url. If someone adds index.php in url it should not work.I have tried many methods . Please give me suggestion. I have tried but it's not working

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

for e.g

if i use below url This should work..............

`example.com/abc`

if i use below url shouldn't work

`example.com/index.php/abc`
user3198563
  • 445
  • 3
  • 13
  • 1
    Please check the below link. Click [https://stackoverflow.com/questions/19183311/codeigniter-removing-index-php-from-url ] – Magento Dev Aug 03 '18 at 04:10
  • If you want simple solution, just rename the `index.php` file to something else and modify the `RewriteRule` to point ro that file, better than writing complex unnecessary rewrite conditions. – Munim Munna Aug 14 '18 at 22:34

8 Answers8

3

Create .htaccess file in your root directory And keep this code into it

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

Open application/config/config.php

    //find the below code   
$config['index_page'] = "index.php" 
//replace with the below code
$config['index_page'] = ""

/find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI" 
suresh
  • 439
  • 3
  • 18
3

In Config.php

$config['base_url'] = 'http://localhost/website/';
$config['index_page'] = '';

# If Live site
# $config['base_url'] = 'http://stackoverflow.com/';

In .htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]

Check my answer on another question Set up the base URL in codeigniter

Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
3

You can use:

RewriteEngine On

# 403 Forbidden for index.php
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^ - [F]

# Rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Croises
  • 18,570
  • 4
  • 30
  • 47
2

Try the following code than..

change the following in config.php

$config['index_page'] = ""
$config['uri_protocol'] = "REQUEST_URI"

Now open .htaccess file and replace with below code..

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

if above index line do not work the replace the line with new code. Because .htaccess depend on server

// Replace last .htaccess line with this line
   RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Avi
  • 1,424
  • 1
  • 11
  • 32
1

Just change your root .htaccess file:

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]

Change in config file:

$config['index_page'] = '';

$config['uri_protocol'] = 'REQUEST_URI';
rob006
  • 21,383
  • 5
  • 53
  • 74
  • are you working in local ? which one you using xamp or wamp ? it work with xamp only and if website is on server my code will work definitely. – Rahul Kalal Aug 03 '18 at 06:44
1

What exactly do you mean by saying url shouldn't work should it redirect to another or be replaced?

url shouldn't work - i think you must say to a user (to a crawler) that there is an error in this link, if the link is already indexed, tell crawler that this is 404 not found page, and it will stop indexing that pages. You can't disable index.php/foo totally, because all requests are going through index.php (i believe, if you are using routing)

Here is replacement example - https://htaccess.madewithlove.be?share=04dcf45c-32e9-5c29-b706-5869cd369ffd

Input url http://ex.com/index.php/abc

RewriteEngine On
RewriteRule index.php/(.*) /404 [L] #send user to not found page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php/$1 [L]

Output url http://ex.com/404

MakeLoveNotWar
  • 956
  • 9
  • 25
  • See the link in my answer – MakeLoveNotWar Aug 16 '18 at 05:06
  • wht will happen . In admin area i am using index.php . what about that ? – user3198563 Aug 17 '18 at 06:04
  • i edited my answer, tested it on my router, when the url is something like `ex.com/index.php/foo/bar/baz` it will request the `ex.com/404` page, but when the url is like `ex.com/foo/bar/baz` it will request the `ex.com/index.php/foo/bar/baz`. So in both cases url will request page through `index.php` which is right i think. I believe you have got one enter point to the website. Give it a try it should work – MakeLoveNotWar Aug 17 '18 at 07:18
  • well it depends on your routing system. – MakeLoveNotWar Aug 28 '18 at 21:32
1

Try with below rule,

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

RewriteRule ^index\.php* - [F]
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
1

Add below code in the top of the index.php. I think this will solve your problem.

if(strpos($_SERVER['REQUEST_URI'],'index.php') !== false){
    header('location: http://xyx.com/404');
    die();
}
Pawnesh Kumar
  • 484
  • 2
  • 10