php配置rewrite模塊

php

(1)    啓用rewrite模塊,在默認狀況下,沒有啓用html

修改httpd.conf文件apache

#啓動rewrite模塊服務器

LoadModule rewrite_module modules/mod_rewrite.soide

 

確認是否啓動成功網站

<?php phpinfo();?>htm

(2)    配置咱們的虛擬主機繼承

httpd.conf 打開虛擬主機的配置文件圖片

 

# Virtual hostsip

Include conf/extra/httpd-vhosts.conf

 

修改 httpd-vhost.conf

<VirtualHost *:80>

    DocumentRoot "C:/myenv/apache/htdocs/static2"

    #Directory配置節點,用於指定該目錄下的文件或是圖片.的訪問權限

    #設置虛擬主機的錯誤頁面,歡迎頁面

    <Directory "C:/myenv/apache/htdocs/static2">

    </Directory>

</VirtualHost>

(3)    在hosts文件中,配置ip和主機的對應關係

127.0.0.1 www.hsp.com

(4)    這時咱們訪問 http//www.hsp.com/news.php

 

咱們能夠訪問到該頁面.

 

☞ 一個重要的知識點:

在apache服務器中,若是某個文件夾,沒有指定訪問權限,則以上級目錄的權限爲準,若是他本身指定了訪問權限,則以本身的爲準.

 

 

請注意,在配置訪問權限的時候,順序很重要:

#Order allow,deny 表示先看allow ,在看deny,留下的就是能夠訪問

    Order deny,allow

    Deny from all

    allow from 127.0.0.1

 

(5)    關於<Directory> 節點配置必須掌握

比較完整的配置文件

第一種配置方式

<VirtualHost *:80>

    DocumentRoot "C:/myenv/apache/htdocs/static2"

    #Directory配置節點,用於指定該目錄下的文件或是圖片.的訪問權限

    #設置虛擬主機的錯誤頁面,歡迎頁面

    ServerName www.hsp.com

    <Directory "C:/myenv/apache/htdocs/static2">

       #這裏能夠指定是否讓人訪問

       #Allow from all

       #是否列出文件目錄結構

       # 若是但願列出 indexes 不但願 none

       #Options indexes

       #如何配置網站的首頁面

       DirectoryIndex abc.html abc2.html

       #如何配置404錯誤頁面,引導用戶引入新頁面

       errorDocument 404 /404.html

       #配置咱們的rewrite規則

       RewriteEngine On

       #rewrite的規則 若是 aaa.html 就跳轉到news.php

       #$1 表示反向引用,第一個子表達式的內容

       #說明若是在正則規範中直接引用子表達式的內容,則使用\n

       #若是是在後面由於,則使用$n

       RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$  news.php?type=$1&id=$2

    </Directory>

</VirtualHost>

 

特別說明: 容易犯的錯誤,必定要記住啓用rewrite模塊.

 

 

思考: 上面咱們配置都要去修改 httpd-vhost.文件,但管理員不給你這個權限,怎麼辦?

思路: 能夠把配置,寫到 .htaccess文件.

 

第二種配置方式: 即把一部分配置放在 http-vhost.conf 文件, 把rewrite 規則放在 .htaccess

<VirtualHost *:80>

    DocumentRoot "C:/myenv/apache/htdocs/static2"

    #Directory配置節點,用於指定該目錄下的文件或是圖片.的訪問權限

    #設置虛擬主機的錯誤頁面,歡迎頁面

    ServerName www.hsp.com

    <Directory "C:/myenv/apache/htdocs/static2">

       #這裏能夠指定是否讓人訪問

       #Allow from all

       #是否列出文件目錄結構

       # 若是但願列出 indexes 不但願 none

       #Options indexes

       #如何配置網站的首頁面

       DirectoryIndex abc.html abc2.html

       #如何配置404錯誤頁面,引導用戶引入新頁面

       errorDocument 404 /404.html

       #若是你配置了allowoverride all 這表示到對應的目錄的.htaccess去匹配規則

       allowoverride all

    </Directory>

</VirtualHost>

 

在對應的文件下 .htaccess文件

<IfModule rewrite_module>

#若是rewrite 模塊啓用

#配置咱們的rewrite規則

RewriteEngine On

#rewrite的規則 若是 aaa.html 就跳轉到news.php

#$1 表示反向引用,第一個子表達式的內容

#說明若是在正則規範中直接引用子表達式的內容,則使用\n

#若是是在後面由於,則使用$n

RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$  news.php?type=$1&id=$2

#RewriteRule aaa.html  news.php

</IfModule>

 

請注意: 項目中的 .htaccess文件的配置也是繼承管理

 

第三種配置方法:

http-vhost.conf

<VirtualHost *:80>

    DocumentRoot "C:/myenv/apache/htdocs/static2"

    #Directory配置節點,用於指定該目錄下的文件或是圖片.的訪問權限

    #設置虛擬主機的錯誤頁面,歡迎頁面

    ServerName www.hsp.com

    <Directory "C:/myenv/apache/htdocs/static2">

       #若是你配置了allowoverride all 這表示到對應的目錄的.htaccess去匹配規則

       allowoverride all

    </Directory>

</VirtualHost>

 

.htacces文件

 

#這裏能夠指定是否讓人訪問

       #Allow from all

       #是否列出文件目錄結構

       # 若是但願列出 indexes 不但願 none

       #Options indexes

       #如何配置網站的首頁面

       DirectoryIndex abc.html abc2.html

       #如何配置404錯誤頁面,引導用戶引入新頁面

       errorDocument 404 /404.html

<IfModule rewrite_module>

#若是rewrite 模塊啓用

#配置咱們的rewrite規則

RewriteEngine On

#rewrite的規則 若是 aaa.html 就跳轉到news.php

#$1 表示反向引用,第一個子表達式的內容

#說明若是在正則規範中直接引用子表達式的內容,則使用\n

#若是是在後面由於,則使用$n

RewriteRule news-([a-zA-Z]+)-id(\d+)\.html$  news.php?type=$1&id=$2

#RewriteRule aaa.html  news.php

</IfModule>

相關文章
相關標籤/搜索