ThinkPHP uses the Rewrite rule of the .htaccess file to hide the index.php implementation in the URL

Add website redirection in gnix

Since the domain name has not been filed, it is not possible to use the implicit url to directly transfer the domain name to another website, so I have no choice but to do it on Vhost. The principle is to visit another website first, and then forward it from this website.

The method is very simple, find the conf file of nginx, the directory is generally in /usr/local/nginx/conf. If you do not do domain name recognition, this file is in nginx.conf under the conf folder. If you do multi-domain name recognition processing, the conf file is in the vhost directory. After opening, add a line

rewrite "^/(.*)$" the domain name to jump to/$1 break;

In this way, all visits will be forwarded to the domain name to be redirected to.

UrlRewrite is what we usually call address rewriting, and all users get are the processed URL addresses.

advantage

One: Improve security, which can effectively prevent some parameter names, IDs, etc. from being completely exposed to the user. If the user arbitrarily input, if it does not meet the rules, a 404 or error page will be returned directly, which is better than directly returning 500 or a large Heap server error messages are much better

Two: Beautify the URL, remove those suffix names such as *.do, long parameter strings, etc., you can organize and simplify the URL that can better reflect the content of the access module
Three: It is more conducive to the income of search engines. Through some optimization of URLs, search engines can better identify and include website information

Scope of use

Address rewriting is generally used to make dynamic addresses pseudo-static . It's not necessary if it's static. After the address is rewritten, the website creator can directly access it by entering the address name.

Thinkphp's .htaccess file configures some rewrite rules for URLs

In the case of thinkphp, there is a .htaccess file, which configures some URL rewriting rules, such as:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]
</IfModule>

What it does is set up URL rewriting to hide the index.php contained in the URL. Generally speaking, URLs that are too long or dynamic are not conducive to SEO, so the purpose of hiding is to achieve better SEO results.
Now that you understand what the .htaccess file does, the next step is to make it work.
For the .htaccess file to work, the server usually needs to enable the URL_REWRITE module to support it.

The following is the configuration process of Apache:

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

2. AllowOverride None Change None to All
    Find "AllowOverride None" in the httpd.conf configuration file and change None to All. It is worth noting that "AllowOverride None" can be found in several places in the file, but only one is to be changed. as the picture shows:

AllowOverride All

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

4. Put the .htaccess file in the same directory as the entry file

ThinkPHP 利用.htaccess文件的 Rewrite 规则隐藏URL中的 index.php实现

去掉 URL 中的 index.php

ThinkPHP 作为 PHP 框架,是单一入口的,那么其原始的 URL 便不是那么友好。但 ThinkPHP 提供了各种机制来定制需要的 URL 格式,配合 Apache .htaccess 文件,更是可以定制出人性化的更利于 SEO 的 URL 地址来。

.htaccess文件是 Apache 服务器中的一个配置文件,它负责相关目录下的网页配置。我们可以利用 .htaccess 文件的 Rewrite 规则来隐藏掉 ThinkPHP URL 中的 index.php 文件(即入口文件),这也是 ThinkPHP URL 伪静态的第一步。

例如原来的 URL 为:

http://127.0.0.1/index.php/Index/insert

去掉 index.php 之后变为:

http://127.0.0.1/Index/insert

如此一来,就变成了 http://服务器地址/应用模块名称/操作名称[/变量参数] 的常见 URL 格式。

更改 Apache httpd.conf 配置文件

提示:如果在虚拟主机商配置,请直接配置第三、四步,因为支持 .htaccess 的空间已经配置好了前面两步。

用编辑器打开 Apache 配置文件 httpd.conf(该文件位于 Apache 安装目录Apache2conf),并按如下步骤修改,。

一、加载了 mod_rewrite.so

确认加载了 mod_rewrite.so 模块(将如下配置前的 # 号去掉):

LoadModule rewrite_module modules/mod_rewrite.so

二、更改 AllowOverride 配置

更改需要读取 .htaccess 文件的目录,将原来的目录注释掉:

#<Directory "C:/Program Files/Apache Group/Apache2/htdocs">
<Directory E:/html/myapp>
更改 AllowOverride None 为 AllowOverride FileInfo Options ,更改后的配置如下所示:
#<Directory "C:/Program Files/Apache Group/Apache2/htdocs">
<Directory E:/html/myapp>
    AllowOverride FileInfo Options
</Directory>

.htaccess 是基于目录来控制的,<Directory E:/html/myapp> 该句即表示需要读取 .htaccess 文件的目录,要根据实际具体 Apache 的解析目录来配置。虚拟主机如果提供 .htaccess 控制,一般都已经配置好了。

三、添加 .htaccess 文件 Rewrite 规则

在需要隐藏 index.php 的目录下(本教程中为 E:/html/myapp,也即入口文件所在目录)创建 .htaccess 文件,并写入如下规则代码:

<IfModule mod_rewrite.c>
RewriteEngine on

#不显示index.php

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

</IfModule>

如果网站已经有 .htaccess 文件,在里面添加该段配置规则即可。如果不能创建该文件(Windows 平台不能创建),可以从本站下载该文件,但该文件仅配置了隐藏 index.php 的规则,点击此处下载

四、更改项目配置文件

编辑项目配置文件 Conf/config.php ,将 URL 模式配置为 2(Rewrite模式):

    'URL_MODEL'=>2,

至此,各个配置已经完成。保存各配置文件后,重启 Apache 服务器并删除 Runtime 目录下的项目缓存文件,在浏览器访问隐藏 index.php 后的地址测试是否成功:

http://127.0.0.1/html/myapp/Index/index

如果访问成功,那么利用 Apache .htaccess 文件的 Rewrite 规则隐藏 index.php 入口文件的配置就成功了。


thinkphp框架在nginx环境下去掉index.php路径显示

将apache下的一个网站迁移到nginx环境中,结果发现网站用的ThinkPHP框架做的开发,默认用的pathinfo。

这是一个很头疼的问题,因为nginx不支持pathinfo,贸然一并打开也担心不安全。

于是查询资料后整理如下:

找到applications/Conf/的配置文件 config.php
return array(
'URL_MODEL'=>2,  //关于URL更多说明请参考Tinkphp/Common/convention.php
);
复制代码
配置 Nginx.conf

在你的虚拟主机下添加

location / {
      if (!-e $request_filename){
           rewrite ^/(.*)$ /index.php/$1 last;
      }
}
复制代码
如果你的项目入口文件在一个子目录内则  // 这一段重写规则相当重要,什么意思呢?

location /目录/ {
      if (!-e $request_filename){
           rewrite ^/目录/(.*)$ /目录/index.php/$1 last;
      }
}

.abu.点评:

测试成功,注意Tinkphp/Common/convention.php
和网站下的applications/Conf/config.php
两个文件 都要修改。

主要就是改成模式2,rewrite方式就可以了。

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326732979&siteId=291194637