Home > php教程 > php手册 > ThinkPHP's URL rewriting + routing + pseudo-static achieves SEO effects.

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

WBOY
Release: 2016-10-17 09:11:15
Original
1392 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, which is responsible for web page configuration in the relevant directory. 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. 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

2. 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,

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 an .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, all configurations have 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 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

<span style="color: #008000">//</span><span style="color: #008000">开启路由器</span>

 'URL_MODEL' => 2,

 'URL_ROUTER_ON' => <span style="color: #0000ff">true</span>,

 'URL_ROUTE_RULES' =>  <span style="color: #0000ff">array</span><span style="color: #000000">(

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

  'test'  => "Home/Index/test", <br>  <span style="color: #008000">//</span><span style="color: #008000">URL/test.html<br><br>  '/^C_(\d+)$/' => 'Index/index/user?id=:1'<br>  //短链接:http://localhost/index.php/C_9</span>
<span style="color: #000000">
 )</span>, 
Copy after login

3. Pseudo-static, add .html suffix 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

at In parameter configuration, you can also configure any other required suffix. Note that the suffix setting includes 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