Sebbert Skrevet 21. september 2011 Del Skrevet 21. september 2011 Denne ble egentlig skrevet av meg til et engelsk forum, så derfor er den på engelsk. Apache Mod_rewrite Tutorial What is Mod_Rewrite? Mod_Rewrite is a way to make URLs point to documents within your Apache server. I.e. you can use it to make "/get578" point to "/actions/get.php?id=578". So, how do you use this? Step 1 Create a .htacces file in the directory you want to point "from". So if you want to make "/pages/hello" point to "/other/hello.php", you place the .htaccess file in "/pages". Step 2 In the .htaccess file, type the following: RewriteEngine OnRewriteOptions Inherit This will start the Mod_rewrite engine, and inherit the settings from httpd.conf. Step 3 For the next part, you'll need a working knowledge of regular expressions. I won't go into very deep detail with this, but if you don't know them, I'd suggest you checkout these sites: http://www.regular-expressions.info/tutorial.html http://forums.digitalpoint.com/showthread.php?t=504361 http://www.easymodrewrite.com/guide-regular-expressions Basically, regex opens for advanced text searching. So, if you want to match upper- and lowercase characters "a-h", you'd use the regex "[a-hA-H]". To make a "pointer" or "Rewrite Rule", add the following: RewriteRule <REGEX> <PAGE> Most of the time, you want to wrap the regex with ^and $. These characters define the beginning and end of the page. So the regex will be: RewriteRule ^<REGEX>& <PAGE> If you don't include these, the regex can be matched anywhere in the url. This is a little example of a Rewrite Rule: RewriteRule ^p([a-fA-F0-9]{5,10})$ page.php?id=$1 Explanation: This regex matches characters a-f uppercase and lowercase, numbers 0-9, between 5 and 10 characters long. The parentheses means the match will be stored in back-reference 1. Then, the $1 in the page means "insert back-reference 1 here". This is the end of this tutorial, for further help, I'd suggest googling "Mod_Rewrite", or checking out some of these pages: http://www.easymodrewrite.com/ http://httpd.apache.org/docs/current/mod/mod_rewrite.html http://www.workingwith.me.uk/articles/scripting/mod_rewrite Thank you for reading this tutorial, I hope it will come in handy! Also, a simple "thanks" would be terrific! 1 Lenke til kommentar
Hårek Skrevet 21. september 2011 Del Skrevet 21. september 2011 Fast regel på forumet: Det holder med én tråd om ditt emne, ikke opprett flere identiske tråder i forskjellige områder av forumet. Lenke til kommentar
Anbefalte innlegg