Thinkphp using the Rewrite rules of the. htaccess file to hide index.php in the URL

Source: Internet
Author: User
Tags php framework

Remove the index.php from the URL

Thinkphp as a PHP framework, is a single entry, then its original URL is not so friendly. But thinkphp provides a variety of mechanisms to customize the desired URL format, in conjunction with Apache. htaccess files, it is possible to customize a more user-friendly SEO URL address.

The. htaccess file is a configuration file in the Apache server that is responsible for the Web page configuration under the relevant directory. We can use the Rewrite rules of the. htaccess file to hide the index.php file in the thinkphp URL (that is, the portal file), which is also the first step of thinkphp URL pseudo-static.

For example, the original URL is:

After removing the index.php, it becomes:

As a result, it becomes a common URL format of http://server address/Application module name/action name [/variable parameter].

Change Apache httpd.conf configuration file

Tip: If you are configuring in a virtual host, configure the third to fourth step directly because the support. htaccess space has been configured for the first two steps.

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

First, load the mod_rewrite.so

Verify that the mod_rewrite.so module is loaded (remove the # number before configuration):

Second, change the allowoverride configuration

Change the directory where the. htaccess file needs to be read and comment out the original directory:

Change allowoverride None to allowoverride FileInfo Options, and the changed configuration is as follows:

. htaccess is a directory-based control, <directory e:/html/myapp> This sentence means that you need to read the directory of. htaccess files, to be configured according to the actual specific Apache parsing directory. If the virtual host is provided with. htaccess control, it is generally configured.

Iii. Add. htaccess file Rewrite rules

Create the. htaccess file in the directory where you want to hide index.php (in this tutorial, the directory where the portal file is located), and write the following rule code:

<ifmodule Mod_rewrite.c>rewriteengine on# does not show Index.phprewritecond%{request_filename}!-dRewriteCond%{REQUEST _filename}!-frewriterule ^ (. *) $ index.php/$1 [qsa,pt,l]</ifmodule>

If the site already has a. htaccess file, add the section configuration rule inside. If the file cannot be created (the Windows platform cannot be created), you can download the file from this site, but the file is only configured with a rule that hides index.php

Iv. changing the project configuration file

Edit the Project Profile conf/config.php, configure the URL pattern to 2 (rewrite mode):

    ' Url_model ' =>2,

At this point, the individual configurations are complete. After saving each configuration file, restart the Apache server and delete the project cache file under the Runtime directory, and the address test after the browser accesses the hidden index.php is successful:

If the access is successful, then the configuration of the index.php portal file using the Rewrite rules of the Apache. htaccess file is successful.

Thinkphp using the Rewrite rules of the. htaccess file to hide index.php in the URL

Related Article

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.