Gå til innhold

En (engelsk) guide til Mod_Rewite i Apache


Anbefalte innlegg

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 On
RewriteOptions 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:

 

 

 

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:

 

 

Thank you for reading this tutorial, I hope it will come in handy! Also, a simple "thanks" would be terrific!

  • Liker 1
Lenke til kommentar
Videoannonse
Annonse
Gjest
Dette emnet er stengt for flere svar.
×
×
  • Opprett ny...