[Intermediate] Effectively delete index.php in the URL

Source: Internet
Author: User

If you are new to CI or have just studied CI's manual, the question of how to effectively remove index.php in URLs to make URLs look friendlier is probably the first more complex question you face! This post is not original, but a synthesis of a variety of opinions! However, the solution proposed in this post can effectively solve the configuration problems in Apache and IIS two environment, but also can effectively avoid the possible no input file specified errors on some virtual Linux virtual hosts. If you're still looking for a solution to the index.php problem, maybe this will be your last stop!


Official Solutions

By default, the index.php file will be included in your URL:
Example.com/index.php/news/article/my_article
You can easily remove it by using the. htaccess file to set some simple rules. The following is an example of using the "negative" method to redirect non-specified content:
HTML Copy Code Rewriteengine on
Rewritecond $!^ (index\.php|images|robots\.txt)
Rewriterule ^ (. *) $/index.php/$1 [L] Copy code
Note: If your project is not in the root directory, please change the above sentence to: rewriterule ^ (. *) $ index.php/$1 [L]
In the above example, any HTTP requests that can implement any non-index.php, images, and robots.txt are directed to index.php.

my ultimate solution but in practice, the above scenario only applies to servers running in the Apache environment and does not have sufficient universal applicability! When the CI program is located on a non-root directory or on some virtual hosts, the above solution causes a "404 error" or "no input File specified "and so on. After the liberation of Baidu reference related issues, we found a common method of effectively removing index.php in URL path, the code reference is as follows:
When index is in the root directory, you can create a new. htaccess file in the root directory where the index.php is located and use the following code:

HTML Copy Code Rewriteengine on
Rewritecond $!^ (index\.php|robots\.txt)
Rewriterule ^ (. *) $/index.php?/$1 [L] Copy code


When index.php is not in the root directory, you can create a new. htaccess file in the directory where index.php is located and use the following code:

HTML Copy Code Rewriteengine on
Rewritecond $!^ (index\.php|images|robots\.txt)
Rewriterule ^ (. *) $/path_to_app/index.php?/$1 [L] Copy code


Note Replace the Path_to_app with the directory where your index.php files are located. Suppose your index.php is placed under the tool subfolder of the root directory, you can write:

HTML Copy Code Rewriteengine on
Rewritecond $!^ (index\.php|images|robots\.txt)
Rewriterule ^ (. *) $/tool/index.php?/$1 [L] Copy code


The above solution should be able to solve the Apache environment how to effectively delete the URL in the index.php problem, if there are other issues, please comment or leave a message and left a valid contact. I will try to resolve it for you and notify you of possible solutions in time!
In addition, you can refer to this article for information on how to effectively remove index.php from URLs in the IIS environment:
IIS7.5 removal of index.php Web. config Profile reference article: How to remove the ultimate solution for index-php in URLs

[Intermediate] Effectively delete index.php in the URL

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.