apache
第一步:找到
#LoadModule rewrite_module modules/mod_rewrite.so
把前面#去掉。没有则添加,但必选独占一行,使apache支持 mod_rewrite 模块
第二步:找到
<Directory "F:/Apache_Workspace/PHP">
#
# Possible values for the Options directive are "None", "All",
# or any combination of:
# Indexes Includes FollowSymLinks SymLinksifOwnerMatch ExecCGI MultiViews
#
# Note that "MultiViews" must be named *explicitly* --- "Options All"
# doesn't give it to you.
#
# The Options directive is both complicated and important. Please see
# http://httpd.apache.org/docs/2.2/mod/core.html#options
# for more information.
#
Options Indexes FollowSymLinks
#
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
#
# Controls who can get stuff from this server.
#
Order allow,deny
Allow from all
</Directory>
把AllowOverride None 换成 AllowOverride All 使apache支持 .htaccess 文件
第三步:重启apache服务器
在要启用伪静态的 PHP 项目根目录下建立 .htaccess 文件
在 .htaccess 文件中输入内容
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule index.html$ index.php
RewriteRule index-([1-9]+[0-9]*).html$ index.php?p=$1
RewriteRule ([a-z]{1,})-([0-9]{1,}).html$ index.php?action=$1&id=$2
</IfModule>
注释:
RewriteEngine 为重写引擎开关,on为开启,off为关闭。
RewriteRule 是路由转向规则,$ 之前路径为浏览器中要输入路径,这里可以用正则表达式表达。$+空格 后路径为后台实际转向路径,转向后台实际路径时可以传参数,例子里的后台页面可以用$_GET['p'] $_GET['action'] $_GET['id'] 来接收
$1 代表浏览器路径中输入的第一个正则表达式的值,以此类推
$2代表第二个正则表达式的值
RewriteRule 路由转向规则里正则表达式用括号 () 括起来
---------------------
作者:lfendo
来源:CSDN
原文:https://blog.csdn.net/u011781521/article/details/68214363
版权声明:本文为博主原创文章,转载请附上博文链接!
nginx
服务器配置文件:
server{
listen 80;
server_name www.abc.com;
root "D:/phpStudy/WWW/abc";
location / {
index index.php index.htm /public/index.html;
autoindex off;
include abc.conf;
#rewrite a.html /index.php/front/index/index last;
}
location ~ \.php(.*)$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
include fastcgi_params;
}
}
伪静态配置文件可以直接写在 location / {} 里面的,不推荐这样做,建议新增加个rewrite.conf写伪静态文件会好点,include 进来就行了,这样可以在rewrite.conf里面写n多配置
location / {
index index.php index.htm /public/index.html;
autoindex off;
include rewrite.conf;
#rewrite a.html /index.php/front/index/index last;
}
1
2
3
4
5
6
#rewrite.conf (这个文件自己创建就行了,文件内容写规则)
#场景一的规则
#http://www.abc.com/index.php/front/index/index
rewrite a.html /index.php/front/index/index last;
#场景二的规则
#1.http://www.abc.com/index.php/front/index/parse/name/yangxignyi/age/18
#2.http://www.abc.com/index.php/front/index/parse?name=yangxignyi&age=18
#3.http://www.abc.com/parse-yangxingyi-18.html
rewrite parse-(\w+)-(\d+).html /index.php/front/index/parse/name/$1/age/$2 last;
---------------------
作者:或非与
来源:CSDN
原文:https://blog.csdn.net/qq_27517377/article/details/79027015
版权声明:本文为博主原创文章,转载请附上博文链接!
注意:nginx链接前必须加/