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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM