(In Nginx and PHP) URL rewriting, and TP implements URL rewriting

Source: Internet
Author: User
In the thinkphp case, there is a. htaccess file with URL rewriting rules configured, such as IfModulemod_rewrite.cRewriteEngineonRewriteCond % {REQUEST_FILENAME }! -DRewriteCond % {REQUEST_FILENAME }! -FRewriteRule ^ (. *) $ index. php $1 [QSA, PT, L] IfModule

In the thinkphp case, there is a. htaccess file with URL rewriting rules configured, such as: IfModule mod_rewrite.c RewriteEngine on RewriteCond % {REQUEST_FILENAME }! -D RewriteCond % {REQUEST_FILENAME }! -F RewriteRule ^ (. *) $ index. php/$1 [QSA, PT, L]/IfModule

In the thinkphp case, there is a. htaccess file with some URL rewriting rules configured, such:


RewriteEngine on
RewriteCond % {REQUEST_FILENAME }! -D
RewriteCond % {REQUEST_FILENAME }! -F
RewriteRule ^ (. *) $ index. php/$1 [QSA, PT, L]

It is used to set URL rewriting to hide index. php contained in the URL. In general, URLs that are too long or dynamic are not conducive to SEO, so the purpose of hiding is to achieve a better SEO effect.
Understand the role of the. htaccess file, and then let it work.
To enable the. htaccess file to work, the URL_REWRITE module must be enabled on the server.

The configuration process of Apache is as follows:

1. The mod_rewrite.so module is loaded in the httpd. conf configuration file.
Search for LoadModule rewrite_module modules/mod_rewrite.so In the httpd. conf configuration file (this is what Apache2 is) and remove the previous #

2. Change AllowOverride None to All
In the httpd. conf configuration file, locate "AllowOverride None" and change "None" to "All. It is worth noting that "AllowOverride None" can be found in the file, but there is only one change. :

AllowOverride All

3. Make sure that the URL_MODEL of the configuration file of the project (usually the foreground project) is set to 2.

4. Place the. htaccess file to the same directory of the entry file.

ThinkPHP uses the Rewrite rules of the. htaccess file to hide index. php In the URL to 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:

#
 
  
 
Change AllowOverride None to AllowOverride FileInfo Options. The changed configuration is as follows:
#
 
  
       AllowOverride FileInfo Options
  
 

. Htaccess is controlled based on directories, This statement indicates that the directory of the. htaccess file needs to be read and 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:

 
  
RewriteEngine on # index. phpRewriteCond % {REQUEST_FILENAME} is not displayed }! -DRewriteCond % {REQUEST_FILENAME }! -FRewriteRule ^ (. *) $ index. php/$1 [QSA, PT, L]
 

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.