본문
Does your web server support mod_rewrite?
Ensure that you are using Apache web server and mod_rewrite module is installed in your web server. To check this you can either contact your hosting service provider or check it yourself using these simple step.
Step1. Create a blank text file using notepad or any other editor.
Step2. Put <?php phpinfo(); ?> in the file.
Step3. Save as ‘info.php’ [on windows Save as "info.php" (with double quotes)].
Step4. Upload the file to your web server’s root or any other folder.
Step5. Call the file in the url ? http://your-domain.com/info.php and check if you see mod_rewrite in ‘Apache loaded modules’ section. If NOT, then please contact your hosting provider and request them to install/enable mod_rewrite.
Note: If you have access to your httpd.conf file, you may check for mod_rewrite in that file as well. And also your httpd.conf must be configured to allow Fileinfo override. Contact your hosting provider for any server related issues.
Next thing you would do is check your .htaccess file. Make a backup of your existing .htaccess file so that in case because of your changes if web server does not serve your site, you can always restore the backup copy.
Examples of mod_rewrite
1. Description ? Your current pages are called using index.php with parameter of url i.e
http://www.example.com/index.php?url=category
and instead of this URL, you want a nice and easy to read URL likehttp://www.example.com/category
Solution ? Put the following lines in your .htaccess file.
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ /index.php?url=$1 [L]
Note: If your file already contains a line ‘RewriteEngine on’ then you don’t need to put it again unless it was set to off before you putting in your lines.
2. Description ? Your current URL is
http://www.example.com/index.php?cat=category&subcat=subcategory
which you would like to see as
http://www.example.com/category/subcategory
Solution ? Put the below lines in your .htaccess file
RewriteEngine on
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?cat=$1&subcat=$2 [L]
3. Description ? You want to have many sub categories or categories like
http://www.your-domain.com/category/subcat1/subcat2/subcat3/subcat4/subcat5/
which you would to rewrite to
http://www.your-domain.com/index.php?cat=category&subcat1=subcat1&subcat2=subcat2 and so on …
Solution ? See below lines..
domain.com/category ?> index.php?cat=category
RewriteRule ^([^/\.]+)/?$ /index.php?cat=$1 [L]
domain.com/category/subcategory/ ?> index.php?cat=category&subcat=subcategory
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ /index.php?cat=$1&subcat=$2 [L]
domain.com/p1/p2/p3/ ?> index.php?a=p1&b=p2&c=p3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?a=$1&b=$2&c=$3 [L]
domain.com/p1/p2/p3/p4 ?> index.php?a=p1&b=p2&c=p3&d=p4
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ /index.php?a=$1&b=$2&c=$3&d=$4 [L]
4. Description ? Your URL has a folder and you would like rewriting for that folder. The URL looks like this http://domain.com/folder/index.php?url=name which you want to see ashttp://domain.com/folder/name/
Solution ? Place the following lines in your .htaccess file
RewriteEngine on
RewriteRule ^folder/([^/\.]+)/?$ folder/index.php?url=$1 [L]
5. Description ? Your actual URL is http://example.com/index.php?page=hello which you want to see as http://example.com/hello.htm
Solution ? Place the following lines in your .htaccess file
RewriteEngine on
RewriteRule ^([^/\.]+).htm$ index.php?page=$1 [L]
6. Description ? Your URL is http://example.com/folder/index.php?page=hello which you want to see as http://example.com/folder/hello.htm
Solution ? Place the following lines in your .htaccess file
RewriteEngine on
RewriteRule ^folder/([^/\.]+).htm$ folder/index.php?page=$1 [L]
There are many more things that you can do with mod_rewrite. As and when I discover more examples, I will keep updating this page. Please feel free to post your usage of mod_rewrite if already not covered here and I will add them to the above list of examples.
Links for further reference:
1. Apache Documentation
2. Wikipedia page for regular expression
관련링크
댓글목록
등록된 댓글이 없습니다.