2016-01-22 27 views
0

我已經通過一些教程,但重寫模塊似乎不是一件容易學習的東西。下面是我從一些教程網站獲得的代碼,但幾乎無法在那裏理解它,我試過它工作正常,它將profile.php的地址更改爲用戶名,我正在查看是否有人能夠簡單地解釋如何重寫wroks in這段代碼。這真的可以幫助我理解這件事情的工作原理。重寫規則如何在我的代碼中工作?

的.htaccess

RewriteEngine On 
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?username=$1 
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?username=$1 

幹嘛friends.php代碼是,如果我想爲friends.php到用戶名/朋友更改地址

回答

1

這裏是如何規定的工作基本描述: -

正則表達式

. (any character) 
* (zero of more of the preceding) 
+ (one or more of the preceding) 
{} (minimum to maximum quantifier) 
? (ungreedy modifier) 
! (at start of string means "negative pattern") 
^ (start of string, or "negative" if at the start of a range) 
$ (end of string) 
[] (match any of contents) 
- (range if used between square brackets) 
() (group, backreferenced group) 
| (alternative, or) 
\ (the escape character itself) 
Using regular expressions, it is possible to search for all sorts of patterns in URLs and rewrite them when they match 

標誌

標誌被添加到重寫規則的末尾告訴Apache如何解釋和處理規則。它們可以用來告訴apache將該規則視爲不區分大小寫,如果當前規則匹配則停止處理規則,或者各種其他選項。它們用逗號分隔,並放在方括號中。以下是標誌的列表及其含義。

C (chained with next rule) 
CO=cookie (set specified cookie) 
E=var:value (set environment variable var to value) 
F (forbidden - sends a 403 header to the user) 
G (gone - no longer exists) 
H=handler (set handler) 
L (last - stop processing rules) 
N (next - continue processing rules) 
NC (case insensitive) 
NE (do not escape special URL characters in output) 
NS (ignore this rule if the request is a subrequest) 
P (proxy - i.e., apache should grab the remote content specified in the substitution section and return it) 
PT (pass through - use when processing URLs with additional handlers, e.g., mod_alias) 
R (temporary redirect to new URL) 
R=301 (permanent redirect to new URL) 
QSA (append query string from request to substituted URL) 
S=x (skip next x rules) 
T=mime-type (force specified mime type) 

例外和特殊情況

重寫條件可以在幾個不同的方式進行測試 - 他們不需要被視爲正則表達式模式,雖然這是他們最常用的方法。這裏有各種方式的rewrite conditons可以加工:

<Pattern (is test string lower than pattern) 
>Pattern (is test string greater than pattern) 
=Pattern (is test string equal to pattern) 
-d (is test string a valid directory) 
-f (is test string a valid file) 
-s (is test string a valid file with size greater than zero) 
-l (is test string a symbolic link) 
-F (is test string a valid file, and accessible (via subrequest)) 
-U (is test string a valid URL, and accessible (via subrequest)) 

link也很有用。

希望它能幫助你:)