Home > php教程 > PHP开发 > body text

ThinkPHP's URL rewriting + routing + pseudo-static achieves SEO effects.

高洛峰
Release: 2016-10-17 09:37:00
Original
1875 people have browsed it

1. URL rewriting, hiding Index.php in the URL.

ThinkPHP, as a PHP framework, has a single entry point, so its original URL is not so friendly. However, ThinkPHP provides various mechanisms to customize the required URL format. With the Apache .htaccess file, it is possible to customize a user-friendly URL address that is more conducive to SEO.

.htaccess file is a configuration file in the Apache server. It is responsible for the configuration of web pages in related directories. We can use the Rewrite rules of the .htaccess file to hide the index.php file (i.e., the entry file) in the ThinkPHP URL. This is also the first step in the pseudo-static ThinkPHP URL.

For example, the original URL is:

http://www.baidu.com/index.php/Index/insert

After removing index.php, it becomes:

http://www.baidu.com/Index /insert

In this way, it becomes the common URL format of http://webpage address/application module name/operation name[/variable parameter].

Change the Apache httpd.conf configuration file

1. Mod_rewrite.so is loaded

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

LoadModule rewrite_module modules/mod_rewrite.so

2 , Change the AllowOverride configuration

Change the directory that needs to read the .htaccess file, and comment out the original directory:

#

Change AllowOverride None to AllowOverride FileInfo Options ,

The changed configuration is as follows:

#

AllowOverride FileInfoOptions

.htaccess is controlled based on the directory. This sentence means that the directory where the .htaccess file needs to be read should be configured according to the actual parsing directory of Apache. If the virtual host provides .htaccess control, it has generally been configured.

3. Add .htaccess file Rewrite rules

Create .htaccess file in the directory where index.php needs to be hidden, and write the following rule code:

RewriteEngine on

#Do not display index.php

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

4. Change the project configuration file

Edit the project configuration file Conf/config.php and configure the URL mode to 2 (Rewrite mode):

'URL_MODEL'=>2,

At this point, each configuration has been completed. After saving each configuration file, restart the Apache server and delete the project cache file in the Runtime directory. Access the address behind the hidden index.php in the browser to test whether it is successful:

http://www.baidu.com/html/myapp/ Index/index

If the access is successful, then using the Rewrite rule of the Apache .htaccess file to hide the configuration of the index.php entry file will be successful.

2. Configure routing to shorten the length of the URL.

Add the following code to the common configuration file

//开启路由器

 'URL_MODEL' => 2,

 'URL_ROUTER_ON' => true,

 'URL_ROUTE_RULES' =>  array(

  'index' => "Home/Index/index",

  'test'  => "Home/Index/test", 
  //URL/test.html

  '/^C_(\d+)$/' => 'Index/index/user?id=:1'
  //短链接:http://localhost/index.php/C_9

 ),
Copy after login

3. Pseudo-static, add the suffix .html to the URL

ThinkPHP supports pseudo-static URL settings, and you can set the required URL suffix. Edit the project configuration file Conf/config.php and configure the
URL_HTML_SUFFIX parameter as follows:
'URL_HTML_SUFFIX' => '.html',

After deleting the project cache file in the Runtime directory to make the configuration effective, access the following address in the browser, The effect is the same:

http://127.0.0.1/html/myapp/index.php/Index/index
http://127.0.0.1/html/myapp/index.php/Index/index.shtml

In the parameters During the configuration, you can also configure any other required suffixes. Note that the suffix settings include the . symbol.
After setting up pseudo-static, in order to keep the URLs consistent everywhere, it is recommended to use the U method to automatically generate URLs in the template. For details, see "ThinkPHP U Method to Automatically Generate URLs".

Tips:
ThinkPHP pseudo-static has an easily misunderstood aspect: after configuring pseudo-static, the system will automatically generate URLs such as xxx.html. The actual situation is not like this, but as mentioned above, after configuring pseudo-static, whether there is a suffix such as .html or not has the same effect as URL access without a suffix. Therefore, if you use pseudo-static, you can only manually add a static suffix or use the ThinkPHP U method to automatically generate a URL address with a suffix. Here, it is of course recommended to use the U method to automatically generate a URL format with a pseudo-static address.


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!