รับทำเว็บไซต์ครบวงจร
เลือกภาษา Thai English     facebook
รายการหลัก



บทความ


Page Ranking Tool

URL RewriteCond


สอน SEO
> URL RewriteCond


The RewriteCond directive

The cannonic documentation about the Apache directives can be found here Apache HTTP Server Version 1.3 - Module mod_rewrite - URL Rewriting Engine. Another very useful help is given in the page Apache HTTP Server Version 2.0 - URL Rewriting Guide.

Important: This page is designed to teach you the basics of the RewriteRule directive. Please refer to the cannonic pages to get a full approach.

The RewriteCond directive allow to specify the condition(s) of an URI rewriting.

RewriteCond TestString CondPattern Flag (optionnal)
“TestString” is the string to be tested. It can be many things, but for a start, we will test Server-Variables. These are variables of the form: 
%{NAME_OF_VARIABLE}
 where NAME_OF_VARIABLE can be a string taken from the following list:

HTTP headers:
HTTP_USER_AGENT (e.g. Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0)
HTTP_REFERER (e.g. The location from which the request has been issued.)
HTTP_COOKIE (e.g. a session cookie)
HTTP_FORWARDED
HTTP_HOST (e.g. www.html4.com)
HTTP_PROXY_CONNECTION
HTTP_ACCEPT (e.g. text/xml,application/xml,application/xhtml+xml,text/html;q=
0.9,text/plain;q=0.8,image/png,*/*;q=0.5)
connection & request:
REMOTE_ADDR (e.g. 82.228.162.118)
REMOTE_HOST (e.g. sp137-1-82-228-162-118.fbx.proxad.net)
REMOTE_USER
REMOTE_IDENT
REQUEST_METHOD (e.g. GET)
SCRIPT_FILENAME (e.g. /home/httpd/vhosts/html4.com/httpdocs/test.php)
PATH_INFO
QUERY_STRING (e.g. the parameters following the HTTP_REFERER - and not “HTTP_REFERER + any parameters” as I wrote here before)
AUTH_TYPE
server internals:
DOCUMENT_ROOT (e.g. /home/httpd/vhosts/html4.com/httpdocs)
SERVER_ADMIN (e.g. webmaster[at]html4.com)
SERVER_NAME (e.g. www.html4.com)
SERVER_ADDR (e.g. 66.98.134.64)
SERVER_PORT (e.g. 80)
SERVER_PROTOCOL (e.g. HTTP/1.1)
SERVER_SOFTWARE (e.g. Apache/2.0.40 (Red Hat Linux))
system stuff:
TIME_YEAR
TIME_MON
TIME_DAY
TIME_HOUR
TIME_MIN
TIME_SEC
TIME_WDAY
TIME
specials:
API_VERSION
THE_REQUEST
REQUEST_URI (e.g. /test.php)
REQUEST_FILENAME
IS_SUBREQ
NOTA : You can match this list against a “phpinfo()” command if you are running a PHP-written site.

I’ve not had to try every variables. Here is the list of variables I’ve used so far :

HTTP_USER_AGENT
HTTP_REFERER
HTTP_HOST
REMOTE_ADDR
REMOTE_HOST
SCRIPT_FILENAME
So our RewriteCond directive will be some kind of:

RewriteCond %{HTTP_USER_AGENT} CondPattern Flag (optionnal)
RewriteCond %{HTTP_REFERER} CondPattern Flag (optionnal)
RewriteCond %{REMOTE_ADDR} CondPattern Flag (optionnal)
RewriteCond %{REMOTE_HOST} CondPattern Flag (optionnal)
RewriteCond %{SCRIPT_FILENAME} CondPattern Flag (optionnal)
Now, let’s see the CondPattern part of it. It’s a bit more complex, for you need to know what a “regular expresion” is. Well, if you do not, you gonna feel lonely. Read this page first :Using Regular Expressions, by Stephen Ramsay - Electronic Text Center - University of Virginia, and then, come back here. In the forthcoming pages, I’ll assume you do know about regular expressions. Your CondPattern can be any regular expression on the considered string.

For instance, to discriminate any user agent containing the string “Frontpage”:

RewriteCond %{HTTP_USER_AGENT} Frontpage
Flags will allow you to tweak the rewriteCond behaviour. For instance, to discriminate any user agent containing the string “Frontpage” without case sensibility, just set the [NC] flag (”NC” for “no case”):

RewriteCond %{HTTP_USER_AGENT} Frontpage [NC]
A flag is optionnal. Between two adjacent RewriteCond directives is an implicit logical AND. You will need the [OR] flag to set a logical OR. For instance, to discriminate any user agent containing the string “Frontpage” without case sensibility (”NC” = no case) AND any referer containing “foo”:

RewriteCond %{HTTP_USER_AGENT} Frontpage [NC]
RewriteCond %{HTTP_REFERER} foo
For instance, to discriminate any user agent containing the string “Frontpage” without case sensibility (”NC” = no case) OR any referer containing “foo”, add an OR flag separated by a comma:

RewriteCond %{HTTP_USER_AGENT} Frontpage [NC,OR]
RewriteCond %{HTTP_REFERER} foo
For instance, to discriminate any user agent containing the string “Frontpage” without case sensibility (”NC” = no case) OR any referer containing “foo” AND a “remote host” ending with “myprovider”:

RewriteCond %{HTTP_USER_AGENT} Frontpage [NC,OR]
RewriteCond %{HTTP_REFERER} foo [OR]
RewriteCond %{REMOTE_HOST} myprovider$
You can specify a non-matching pattern with a “!” sign before it. For instance, to discriminate any user agent containing the string “Frontpage” without case sensibility (”NC” = no case) OR any referer containing “foo” AND a “remote host” NOT ending with “myprovider”:

RewriteCond %{HTTP_USER_AGENT} Frontpage [NC,OR]
RewriteCond %{HTTP_REFERER} foo [OR]
RewriteCond %{REMOTE_HOST} !myprovider$
And so on…


Short URL click!
<< กลับคืน : เข้าชม 5,257 ครั้ง : ขึ้นไปด้านบน

รับทำเว็บไซต์ รับสร้างเว็บไซต์ รับออกแบบเว็บ รับเขียนเว็บ รับสอนทำเว็บ รับเช่า hosting รับเช่าพื้นที่เว็บไซต์ จดชื่อเว็บ รับโปรโมทเว็บไซต์ รับดูแลเว็บ SiteMap
สปริงเกอร์http://www.xn--22c2c4blb9n.xn--o3cw4h/