伪静态的核心是Apache的mod_rewrite
模块,它允许我们重写客户端请求的URL,而不需要实际存在对应的物理文件,默认下mod_rewrite
模块处于关闭状态,即手动配置开启。
操作步骤:
1、进入Apache安装目录,找到配置文件httpd.conf(如F:\Server\Apache24\conf\httpd.conf
),编辑该文件,找到:
#LoadModule rewrite_module modules/mod_rewrite.so
把前面的“#”去掉取消注释,修改为:
LoadModule rewrite_module modules/mod_rewrite.so
PS:如果没有找到这一行,可以直接复制上面的代码手动添加这行代码。
2、找到定义网站根目录的<Directory>
,如:
<Directory "${SRVROOT}/htdocs"> # # 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.4/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: # AllowOverride FileInfo AuthConfig Limit # AllowOverride None # # Controls who can get stuff from this server. # Require all granted </Directory>
把AllowOverride None
修改为AllowOverride all
,修改后的代码如下:
<Directory "${SRVROOT}/htdocs"> # # 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.4/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: # AllowOverride FileInfo AuthConfig Limit # AllowOverride All # # Controls who can get stuff from this server. # Require all granted </Directory>
说明:
AllowOverride None:表示 Apache 完全忽略
.htaccess
文件。AllowOverride All:表示允许
.htaccess
文件覆盖 Apache 的配置,这是使用mod_rewrite
规则所必需的。
3、以管理员身份打开命令提示符,运行下面的命令重启Apache服务器:
net stop apache2.4 && net start apache2.4
或进入Apache的bin目录执行:
./httpd.exe -k restart
Apache重启成功后,就可以使用.htaccess
文件添加伪表态规则了。
PS:.htaccess
文件则是一个分布式配置文件,可以放在任何目录中,来为该目录及其所有子目录定义重写规则。