1. 程式人生 > >apache url rewrite實現url重寫配置詳解

apache url rewrite實現url重寫配置詳解

如果你是apache伺服器要實現url rewrite重寫我們需要在apache中開啟mod_rewrite.so配置,下面我把具體過程給各位同學詳細介紹介紹。

配置步驟:

第一步:找到apache的配置檔案httpd.conf(檔案在conf目錄下)

第二步:你首先必須得讓伺服器支援mod_rewrite,如果你使用的是虛擬主機,請事先詢問你的主機提供商。

開啟httpd.conf,找到

 程式碼如下 複製程式碼

#LoadModule rewrite_module modules/mod_rewrite.so


把#去掉

找到AllowOverride None 改成 AllowOverride All,

注:AllowOverride 的引數設定為ALL,表示整臺伺服器上都支援URL規則重寫。Apache 伺服器要讀每個網站下目錄下的 .htaccess 檔案。如果沒有這個檔案,或者這個文件沒有定義任何關於URL重寫的規則就不會有任何效果。

對於不同的網址,需要在APACHE中增加如下內容

 程式碼如下 複製程式碼

RewriteEngine On
RewriteMap lowercase int:tolower
RewriteCond ${lowercase:%{SERVER_NAME}} ^[a-z0-9]+.eact.com.cn$
RewriteRule ^(.+) ${lowercase:%{SERVER_NAME}}$1 [C]
##RewriteRule ^u.eact.com.cn/(.*) /ux/index.html [L]
RewriteRule ^([a-z0-9]+).eact.com.cn/(.*) /$1/$2 [L]

例項

例項301跳轉

例一.下面是在一個虛擬主機裡定義的規則。功能是把client請求的主機字首不是www.111cn.net和70.40.213.183都跳轉到主機字首為http://www.111cn.net,避免相同內容的網頁有多個指向的域名,如http://111cn.net。

 程式碼如下 複製程式碼

NameVirtualHost 70.40.213.183:80
ServerAdmin [email protected]
DocumentRoot “/web”
ServerName 111cn.net

RewriteEngine on #開啟rewirte功能
RewriteCond %{HTTP_HOST} !^www.111cn.net [NC] #宣告Client請求的主機中字首不是www.111cn.net,其中 [NC] 的意思是忽略大小寫
RewriteCond %{HTTP_HOST} !^70.40.213.183 [NC] #宣告Client請求的主機中字首不是70.40.213.183,其中 [NC] 的意思是忽略大小寫
RewriteCond %{HTTP_HOST} !^$ #宣告Client請求的主機中字首不為空
RewriteRule ^(.*) http://www.111cn.net/ [L] #含義是如果Client請求的主機中的字首符合上述條件,則直接進行跳轉到http://www.111cn.net/,[L]意味著立即停止重寫操作,並不再應用其他重寫規則。這裡的.*是指匹配所有URL中不包含換行字元,()括號的功能是把所有的字元做一個標記,以便於後面的應用.就是引用前面裡的 (.*)字元。

例二.將輸入 en.111cn.net 的域名時跳轉到www.111cn.net

RewriteEngine on
RewriteCond %{HTTP_HOST} ^en.111cn.net [NC]
RewriteRule ^(.*) http://www.111cn.net/ [L]


實現 偽靜態在每個虛擬主機的地方設定Rewrite引數

 程式碼如下 複製程式碼

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot "I:/Job/Java"
ServerName localhost
RewriteEngine on
RewriteRule /([0-9]+).html /test.jsp?id=$1 [PT]
RewriteRule /([0-9]+)_([0-9]+).html /content.jsp?id=$1&id2=$2 [PT]
</VirtualHost>

apache設定靜態檔案快取方法介紹

www.111cn.net 更新:2013-07-06 編輯:Win2000 來源:轉載 在apache中我們要實現快取檔案需要開戶mod_expires模組,快取功能實現之後我們可以提升伺服器的效能(縮短服務的響應時間、減輕伺服器負擔、減少網路頻寬使用量)這裡就不一一介紹了。

為了減少客戶端對服務端資源的請求,可以開啟mod_expires.so模組

apache%C5%E4%D6%C3/" rel="nofollow" target="_blank">apache配置檔案中去掉這段

“#LoadModule expires_module modules/mod_expires.so

”前面的#號


Httpd.conf配置

 程式碼如下 複製程式碼

ExpiresActive on

#快取3天。

ExpiresBytype text/css "access plus 3 days

ExpiresByType application/x-javascript "access plus 3 days "

ExpiresByType image/jpeg "access plus 3 days "

Expiresbytype image/gif "access plus 3 days "


加入下面內容

 程式碼如下 複製程式碼
<IfModule mod_expires.c>
# mod_expires
ExpiresActive on
ExpiresDefault A864000
ExpiresBytype text/css “access plus 14 days
ExpiresByType text/javascript “access plus 14 days ”
ExpiresByType application/x-javascript “access plus 14 days ”
ExpiresByType application/x-shockwave-flash “access plus 14 days ”
ExpiresByType image/* “access plus 14 days ”
ExpiresByType text/html “access plus 14 days ”
<FilesMatch “.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav|jpg|gif)$”>
ExpiresDefault A864000
</FilesMatch>
</IfModule>

預設快取時間是10天,css快取時間是14天

Apache搭建多個站點方法詳解

www.111cn.net 更新:2013-06-28 編輯:Bolshevik 來源:轉載 Apache的虛擬主機是一種允許在同一臺機器上配置多個不同站點的web伺服器環境的,就是iis一樣可以建立多站點了,但是apache需要在編輯狀態操作,不能像windows iis直接點選幾下就好了,下面我來給各位介紹配置方法。

最平常的大概有3種方法。

第一種:單IP不同埠

第二種:多IP同埠(獨立IP的虛擬空間)

第三種:域名繫結根目錄的方式(共享IP的虛擬空間)


Apache的核心配置檔名是”httpd.conf”,其所存放的路徑在Apache目錄下的conf資料夾下。修改它只需要使用記事本(建議使用其他編輯器,帶行數的那種,方便修改),生效的話只需要儲存httpd.conf,重啟apache即可。

下面多站點支援的話,修改httpd.conf的第187~264行(不同的httpd.conf可能有差異),也就是在ServerAdmin和ServerName那裡,大部分是註釋。下面是主要修改的地方。

注意:如果是伺服器請備份httpd.conf後再修改檔案。

 程式碼如下 複製程式碼

# 'Main' server configuration
#
# The directives in this section set up the values used by the 'main'
# server, which responds to any requests that aren't handled by a
# <VirtualHost> definition.  These values also provide defaults for
# any <VirtualHost> containers you may define later in the file.
#
# All of these directives may appear inside <VirtualHost> containers,
# in which case these default settings will be overridden for the
# virtual host being defined.
#

#
# ServerAdmin: Your address, where problems with the server should be
# e-mailed.  This address appears on some server-generated pages, such
# as error documents.  e.g. [email protected]
#
ServerAdmin [email protected]

#
# ServerName gives the name and port that the server uses to identify itself.
# This can often be determined automatically, but we recommend you specify
# it explicitly to prevent problems during startup.
#
# If your host doesn't have a registered DNS name, enter its IP address here.
#
ServerName www.example.com:80

#
# Deny access to the entirety of your server's filesystem. You must
# explicitly permit access to web content directories in other
# <Directory> blocks below.
#
<Directory />
    AllowOverride All
    Require all denied
</Directory>

#
# Note that from this point forward you must specifically allow
# particular features to be enabled - so if something's not working as
# you might expect, make sure that you have specifically enabled it
# below.
#

#
# DocumentRoot: The directory out of which you will serve your
# documents. By default, all requests are taken from this directory, but
# symbolic links and aliases may be used to point to other locations.
#
DocumentRoot "g:/www"
<Directory "g:/www">
    #
    # 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:
    #   Options FileInfo AuthConfig Limit
    #
    AllowOverride All

    #
    # Controls who can get stuff from this server.
    #
    Require all granted
</Directory>

第一種一般是測試環境,畢竟加了埠,如何繫結域名,訪問的時候域名後面也需加埠。

例子分別通過80和8080訪問不同的根目錄。

大概在50幾行有個Listen 80,在下面新增8080埠。

 程式碼如下 複製程式碼

Listen 80
Listen 8080<VirtualHost *:80>
    ServerAdmin [email protected]
    ServerName localhost:80
    DocumentRoot "g:/www1"
     <Directory "g:/www1">
     Options  Indexes FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost *:8080>
    ServerAdmin [email protected]
    ServerName localhost:8080 
    DocumentRoot "g:/www2"
   <Directory "g:/www2">
     Options Indexes FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

第二種多IP同埠。

IP地址1:192.168.2.2

IP地址2:192.168.1.68

埠同是80埠。

 程式碼如下 複製程式碼

<VirtualHost 192.168.1.68:80>
    ServerAdmin [email protected]
    ServerName localhost:80
    DocumentRoot "g:/www1"
     <Directory "g:/www1">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost 192.168.2.2:80>
    ServerAdmin [email protected]
    ServerName localhost:80
    DocumentRoot "g:/www2"
   <Directory "g:/www2">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>

第三種同IP不同域名和根目錄(域名的話修改本地host演示)。

 程式碼如下 複製程式碼


<VirtualHost 192.168.2.2:80>
    ServerAdmin [email protected]
    ServerName www.111cn.net
    DocumentRoot "g:/www1"
     <Directory "g:/www1">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>  
</VirtualHost>
<VirtualHost 192.168.2.2:80>
    ServerAdmin [email protected]
    ServerName www.111cn.net
    DocumentRoot "g:/www2"
   <Directory "g:/www2">
     Options FollowSymLinks
     AllowOverride All
     Require all granted
   </Directory>      
</VirtualHost>