I had wrote Simple php guestbook based on MVC last day, and want to refine the mvc framework and make my own blog system.In that post i refered the htaccess file,It is very powerfule for developers,today i want to summary something about it and some useful rewrite rule write in htaccess file.
Below is one snippet write in wordpress’s htaccess file that make the url to be showed link www.domain.com/posttitle, i think it will be very useful in my future blog, so i will analysis it here.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
1.<IfModule mod_rewrite.c></pre> does the work checking if the rewrite module exists, most like the if statement in php.
2.The second line and the third line is to open the rewrite module and set the rewrite directory.
3.Next line command will rewrite the url to hide the index.php when the url consists of index.php
4.The remaining lines decide when the files or the directories visited exist, you will go to the url; if not you will be jump to the index.php
You will notice that there a [L],-f,-d after the commands, here are some introduction:
RewriteCond Syntax: RewriteCond TestString CondPattern [flags]
[L]: the last command be executed, that mean the commands below will be ignored except the right rewrite comditions will cause executing the last commands.
[NC]: With this in the command , letters in the url will not not be case-sensitive
-f: check if is regular file
-d: check it is directory and if the directory exists
And that is the wordpress htaccess file.Other examples of htaccess RewriteRule and htaccess RewriteCond:
1.According to the user agent , the server decides which file to visit, is it like a if statement?
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} ^Mozilla/5.0.*
RewriteRule index.php index.m.php
RewriteCond %{HTTP_USER_AGENT} ^Lynx.*
RewriteRule index.php index.L.php
RewriteRule index.php index.b.php
2.If the host of visitor’s last visited page is www.test.cn , the current request to any page will be redirect to test.php
RewriteCond %{HTTP_REFERER} (www.test.cn)
RewriteRule (.*)$ test.php
3.If your ip address is host1 or host2 or host3, you will be jump to test.php.Here we can see that the default relativeship of each line RewriteCond is ‘and’ , if you want to use or you must write is clearly.
RewriteCond %{REMOTE_HOST} ^host1.* [OR]
RewriteCond %{REMOTE_HOST} ^host2.* [OR]
RewriteCond %{REMOTE_HOST} ^host3.*
RewriteRule (.*)$ test.php
4.If the files end up with those suffixes, the url will not rewrite and visit that file directly.
RewriteCond %{REQUEST_URI} !^.*(.css|.js|.gif|.png|.jpg|.jpeg)$
5.Preventing images hotlinking
RewriteCond %{HTTP_REFERER} !^$ [NC] // alow visit images by visiting the image url
RewriteCond %{HTTP_REFERER} !dev-trickss.com [NC] // allow this domain to visit the images
RewriteRule .*\.(gif|jpg|png)$ - [F] // besides the allowed domain, other domain will display crosses instead of the images
6.301 redirect, this will be very useful to do seo and some status like 302,404 ect. More response status reffer here.
RewriteRule ^index\.php$ http://dev-trickss.com/ [R=301,L]
7.Custom Error Pages
ErrorDocument 401 /err/401.php
ErrorDocument 403 /err/403.php
ErrorDocument 404 /err/404.php
8.Forcing PHP to include files from a specific directory, that means you can set a specific directory, that when you use include in php, the program will search the file directly to the specific directory.
php_value include_path /home/myaccount/library
With this setted, you can use the include function like this in any where.
include('developer.php');
9.hide some directories you do not want to be known by public and the spider
deny from all
//deny specific IP addresses
order allow,deny
deny from 123.48.56.108
allow from all
//allow only specific IP addresses
order deny,allow
deny from all
allow from 123.48.56.108
10.have you wish to execute a little php code in you stylesheet?You can realize this by setting commands in the htaccess file.
<FilesMatch ".(css|style)$">
SetHandler application/x-httpd-php
</FilesMatch>
You can find the usage manual here:Module mod_rewrite, and for Chinese can find the translation here:杨宇的技术博客.
I have summaried all i can find here, many of these methods will not be very useful, but still you can not abandon them, for me,the most useful code will be that of wordpress’s htaccess commands.