apache2.4.33伪静态配置入门教程(1)

伪静态: 把动态网页的请求方式伪装成静态网页

要使用伪静态技术,要在httpd.conf中启用伪静态模块: 

LoadModule rewrite_module modules/mod_rewrite.so

把前面的#号去掉

通常利用Apache的rewrite模块对 URL 进行重写的时候, rewrite规则会写在 .htaccess 文件里。但要使 apache 能够正常的读取.htaccess 文件的内容,就必须对.htaccess 所在目录进行配置。从安全性考虑,根目录的AllowOverride属性一般都配置成不允许任何Override ,即

<Directory />
    AllowOverride None
    Require all denied
</Directory>

一、在 AllowOverride 设置为 None 时, .htaccess 文件将被完全忽略, 当此指令设置为 All 时,apache才会读取.htaccess中的规则并应用

我的DocumentRoot:

DocumentRoot "/www"
root@dev:/www# ls -a
.  ..  .htaccess  index.htm
root@dev:/www# cat .htaccess 
RewriteEngine on
RewriteRule ^(.*).html$ $1.htm
root@dev:/www# cat index.htm 
<h3>this is index.htm</h3>
root@dev:/www# 

.htaccess规则详解:

RewriteEngine on #开启伪静态

RewriteRule ^(.*).html$ $1.htm #当访问以.html结尾的文件时,会被路由到(.*).htm的地址,比如:

访问http://localhost/index.html -----> 会被规则解释为http://localhost/index.htm,这样读到的内容是www目录下的index.htm文件的内容

要使伪静态生效,需要对/www 目录设置为

Options Indexes FollowSymLinks
AllowOverride All Require all granted

 我的httpd.conf配置:

1,去除httpd.conf中的注释和空行

grep -v "^s*#" httpd.conf | grep -v "^$" > httpd.conf.bak

2,httpd.conf完整配置:

ServerRoot "/usr/local/httpd24"
Listen 80
LoadModule mpm_prefork_module modules/mod_mpm_prefork.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule access_compat_module modules/mod_access_compat.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule reqtimeout_module modules/mod_reqtimeout.so
LoadModule filter_module modules/mod_filter.so
LoadModule mime_module modules/mod_mime.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule version_module modules/mod_version.so
LoadModule unixd_module modules/mod_unixd.so
LoadModule status_module modules/mod_status.so
LoadModule autoindex_module modules/mod_autoindex.so
<IfModule !mpm_prefork_module>
</IfModule>
<IfModule mpm_prefork_module>
</IfModule>
LoadModule dir_module modules/mod_dir.so
LoadModule alias_module modules/mod_alias.so
LoadModule rewrite_module modules/mod_rewrite.so
<IfModule unixd_module>
User daemon
Group daemon
</IfModule>
ServerAdmin you@example.com
ServerName 127.0.0.1
<Directory />
    AllowOverride None
    Require all denied
</Directory>
DocumentRoot "/www"
<Directory "/www">
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
<IfModule dir_module>
    DirectoryIndex index.html
</IfModule>
<Files ".ht*">
    Require all denied
</Files>
ErrorLog "logs/error_log"
LogLevel warn
<IfModule log_config_module>
    LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i"" combined
    LogFormat "%h %l %u %t "%r" %>s %b" common
    <IfModule logio_module>
      LogFormat "%h %l %u %t "%r" %>s %b "%{Referer}i" "%{User-Agent}i" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access_log" common
</IfModule>
<IfModule alias_module>
    ScriptAlias /cgi-bin/ "/usr/local/httpd24/cgi-bin/"
</IfModule>
<IfModule cgid_module>
</IfModule>
<Directory "/usr/local/httpd24/cgi-bin">
    AllowOverride None
    Options None
    Require all granted
</Directory>
<IfModule headers_module>
    RequestHeader unset Proxy early
</IfModule>
<IfModule mime_module>
    TypesConfig /etc/httpd24/mime.types
    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz
</IfModule>
<IfModule proxy_html_module>
Include /etc/httpd24/extra/proxy-html.conf
</IfModule>
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

对于伪静态,主要注意伪静态模块是否加载,Directory下面的设置

二,也可以把伪静态规则写在apache配置文件的Directory段,为了验证配置的作用,可以先把.htaccess删除或者重命名

root@dev:/www# ls -a
.  ..  .htaccess.bak  index.htm
<Directory "/www">
    Options Indexes FollowSymLinks
    AllowOverride none
    RewriteEngine on
    RewriteRule ^(.*).html$ $1.htm
    Require all granted
</Directory>

即使AllowOverride 设置为none,伪静态依然生效,注意修改完apache的配置文件需要重启apache服务器

/usr/local/httpd24/bin/apachectl restart
原文地址:https://www.cnblogs.com/ghostwu/p/9048816.html