Thinkphp uses the rewrite rules of the. htaccess file to hide index. php In the URL.

Source: Internet
Author: User
Remove index. php from the URL

Thinkphp, as a PHP framework, is a single portal, so its original URL is not so friendly. However, thinkphp provides various mechanisms to customize the desired URL format. In combination with the Apache. htaccess file, thinkphp can also customize a user-friendly and Seo-friendly URL address.

The. htaccess file is a configuration file on the Apache server. It is responsible for the webpage configuration under the relevant directory. We can use the rewrite rules of the. htaccess file to hide the index. php file (that is, the entry file) in the thinkphp URL. This is also the first step of the pseudo-static thinkphp URL.

For example, the original URL is:

Http: // 127.0.0.1/index. php/index/insert

After removing index. php, it becomes:

Http: // 127.0.0.1/index/insert

As a result, it becomes a common URL format of http: // server address/application module name/operation name [/Variable Parameter.

Change Apache httpd. conf configuration file

Tip: If the configuration is performed on the VM provider, configure Steps 3 and 4 directly, because the space supporting. htaccess has been configured in the previous two steps.

Open the Apache configuration file httpd. conf in the editor (the file is located in the Apache installation directory apache2conf) and follow the steps below to modify the file ,.

I. Loaded mod_rewrite.so

Confirm that the mod_rewrite.so module is loaded (remove the # Before the following configuration ):

Loadmodule rewrite_module modules/mod_rewrite.so
Ii. Change AllowOverride Configuration

Change the directory for reading the. htaccess file and comment out the original directory:

# <Directory "C:/program files/Apache Group/apache2/htdocs"> <directory e:/html/MyApp>

Change AllowOverride none to AllowOverride fileinfo options. The changed configuration is as follows:

# <Directory "C:/program files/Apache Group/apache2/htdocs"> <directory e:/html/MyApp> AllowOverride fileinfo options </directory>

. Htaccess is controlled based on the directory. <directory e:/html/MyApp> indicates that the statement needs to be read. the Directory of the htaccess file should be configured according to the actual Apache parsing directory. If the VM provides. htaccess control, it is generally configured.

3. Add Rewrite Rules for. htaccess files

Create a. htaccess file in the directory where index. php needs to be hidden (E:/html/MyApp in this tutorial, that is, the directory where the entry file is located) and write the following rule code:

<Ifmodule mod_rewrite.c> rewriteengine on # index. phprewritecond % {request_filename} is not displayed }! -Drewritecond % {request_filename }! -Frewriterule ^ (. *) $ index. php/$1 [QSA, PT, l] </ifmodule>

If the website already has a. htaccess file, add the configuration rules for this section. If the file cannot be created (cannot be created on Windows), you can download the file from this site. However, this file only configures rules for hiding index. php. Click here to download the file.

4. Change the project configuration file

Edit the project configuration file CONF/config. php and set the URL mode to 2 (rewrite mode ):

'Url _ model' => 2,

So far, the configuration has been completed. After saving the configuration files, restart the Apache server and delete the project cache files under the runtime directory. Then, access and hide the index. php address in the browser to test whether the files are successful:

Http: // 127.0.0.1/html/MyApp/index/Index

If the access is successful, you can use the rewrite rules of the Apache. htaccess file to hide the configuration of the index. php entry file.

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.