How can remove index.php from url [duplicate]

2019-01-20 16:29发布

问题:

This question already has an answer here:

  • Remove index.php of codeigniter 3 answers

when I mouse over or click on anchor links then it shows

http://localhost/code-testing/index.php/about-us. 

I tried base_url() and site_url() too but the result is same.how can I remove that 'index.php' from the url ?

-Thanks.

Resolved:

Thank you everybody for your kind answers. I'm using xampp and this one worked for me

code-testing/application/config/config.php

$config['base_url']= 'http://localhost/code-testing/';
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

code-testing/.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]

and the following links then worked without showing 'index.php' in the url

<li><a href="<?php echo base_url('home');?>">HOME</a></li>
<li><a href="<?php echo base_url('about_us');?>">About Us</a></li>
<li><a href="<?php echo base_url('contact');?>">Contact Us</a></li>

回答1:

There are 3 steps to remove index.php

1.Make below changes in application/config.php file

$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';

2.Make .htacces file in your root directory using 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]

3.Enable rewrite mode

i. First, initiate it with the following command:

a2enmod rewrite

ii. Edit the file /etc/apache2/sites-enabled/000-default

change the AllowOverride None to AllowOverride All.

iii. Restart your server with the following command:

sudo /etc/init.d/apache2 restart



回答2:

Set the variable empty as below.

$config['index_page'] = '';

try to replace following variable with these parameters ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') one by one

$config['uri_protocol'] = 'AUTO';


回答3:

Have the.htaccess file in the application root directory, along with the index.php file. (Check if the htaccess extension is correct , Bz htaccess.txt did not work for me.)

And Add the following rules to .htaccess file,

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

Then find the following line in your application/config/config.php file

$config['index_page'] = 'index.php';

Set the variable empty as below.

$config['index_page'] = '';

That's it, it worked for me.

If it doesn't work further try to replace following variable with these parameters ('AUTO', 'PATH_INFO', 'QUERY_STRING', 'REQUEST_URI', and 'ORIG_PATH_INFO') one by one

$config['uri_protocol'] = 'AUTO';