mod_rewrite
Upcoming SlideShare
Loading in...5
×
 

mod_rewrite

on

  • 973 views

In Dutch.

In Dutch.
Based on http://www.slideshare.net/scriptfu/modrewrite-2225412

Statistics

Views

Total Views
973
Views on SlideShare
972
Embed Views
1

Actions

Likes
0
Downloads
10
Comments
0

1 Embed 1

http://www.docshut.com 1

Accessibility

Categories

Upload Details

Uploaded via as Apple Keynote

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n

mod_rewrite mod_rewrite Presentation Transcript

  • mod_rewrite Bert Pattyn May 2011
  • Wat?• Apache module• Zwitsers mes voor URL manipulatie• op server niveau: httpd.conf• op directory niveau: .htaccess
  • Waarom gebruiken?• Verbergen van de echte URLs• Propere URLs• SEO (oh yeah...)• Redirect naar andere pagina• Toegangscontrole
  • Geïnstalleerd?Zet volgende in .htaccess RewriteEngine OnError indien niet beschikbaar
  • Directives• RewriteEngine• RewriteCond• RewriteRule• RewriteOption• RewriteLog• RewriteBase• en meer!
  • Voorbeeld 1: ForkRewriteEngine OnRewriteBase /RewriteCond %{REQUEST_URI} ^/backend/$RewriteRule . index.php [NC,L]RewriteCond %{REQUEST_URI} !^$RewriteCond %{REQUEST_FILENAME} !-fRewriteCond %{REQUEST_FILENAME} !-dRewriteRule . index.php [NC,L]
  • Voorbeeld 1: Fork# activeer Rewriting EngineRewriteEngine On# base alle rewrite op de ‘/’ URLRewriteBase /# backend is een bestaande directory die behandeld moet worden# als ieder andere fileRewriteCond %{REQUEST_URI} ^/backend/$RewriteRule . index.php [NC,L]# als de URL niet leeg isRewriteCond %{REQUEST_URI} !^$# ... en het bestand bestaat nietRewriteCond %{REQUEST_FILENAME} !-f# ... en de directory bestaat nietRewriteCond %{REQUEST_FILENAME} !-d# rewrite de url naar /index.phpRewriteRule . index.php [NC,L]
  • RewriteEngine On• activeert de rewriting engine• verplicht als eerste regel voor alle andere regels
  • RewriteRule pattern result [flags]• kan meer dan 1 keer voorkomen• worden in volgorde afgehandeld• eerste regel wordt toegepast op de volledige URL• volgende regels gebruiken het resultaat van de vorige regel
  • RewriteRule pattern result [flags]• PCRE (Perl-Compatible Regular Expressions)• vergelijkt met het URL gedeelte (zonder domein)• RewriteCond nodig om andere stukken te vergelijken
  • RewriteRule pattern result [flags]kan één van de volgende items zijn:• filesystem path ( /kip/ei.pdf )• URL path• volledige URL (met domein)• -
  • RewriteRule pattern result [flags]• beïnvloedt het gedrag van de Rule• tussen vierkante haakjes [NC]• meerdere flags gescheiden door komma’s [NC, QSA, L]
  • RewriteRule pattern result [flags]
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteRule pattern result [flags] C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Don’t process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • RewriteCond variable pattern [flags]• HTTP Server Variables (HTTP_HOST, REQUEST_URI)• pattern wordt vergeleken met de waarde van deze var• flags die het gedrag van de Condition veranderen (net zoals bij een RewriteRule)
  • RewriteCond variable pattern [flags]
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 1. Fork (opnieuw)# activeer Rewriting EngineRewriteEngine On# base alle rewrite op de ‘/’ URLRewriteBase /# backend is een bestaande directory die behandeld moet worden# als ieder andere fileRewriteCond %{REQUEST_URI} ^/backend/$RewriteRule . index.php [NC,L]# als de URL niet leeg isRewriteCond %{REQUEST_URI} !^$# ... en het bestand bestaat nietRewriteCond %{REQUEST_FILENAME} !-f# ... en de directory bestaat nietRewriteCond %{REQUEST_FILENAME} !-d# rewrite de url naar /index.phpRewriteRule . index.php [NC,L]
  • 2. non-www naar wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^example.com$ [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] pattern flags result
  • 2. non-www naar www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]vergelijk met deze patternpattern binnen ronde haakjes variable ‘$1’
  • 2. non-www naar wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^example.com$ [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] vervang door “http://www.example.com”
  • 2. non-www naar wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^example.com$ [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] de waarde die binnen de ronde haakjes is opgevangen
  • 2. non-www naar wwwRewriteEngine OnRewriteCond %{HTTP_HOST} ^example.com$ [NC]RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Redirect met een 301 code Maak dit de laatste RewriteRule
  • 3. no porn guys on sundayRewriteEngine OnRewriteCond %{TIME_WDAY} 0RewriteCond %{HTTP_REFERER} (porn) [NC]RewriteRule .* - [F]
  • 4. test stadiumRewriteEngine OnRewriteCond %{REMOTE_ADDR} !^8.82.245.147$RewriteRule .* /temp/index.html [R, NC, L]
  • RewriteOptions inherit• neem de config over van de parent• in virtual server context, neem alle rules over die op serverniveau gedefinieerd staan• in directory context, neem alle context, condities en rules over die gedefinieerd staan in de parent
  • RewriteBase path• zet de basis voor de rewrites
  • RewriteBase path# /abc/def/.htaccess -- config file voor de directory /abc/def# Belangrijk: /abc/def is het fysieke path voor /xyz# de server heeft een Alias /xyz /abc/def directiveRewriteEngine On# laat de server weten dat we komen via /xyz en niet# via het fysieke path /abc/defRewriteBase /xyz# the rewriting rulesRewriteRule ^oldstuff.html$ newstuff.html# /xyz/oldstuff.html wordt hier dus herschreven naar /abc/def/newstuff.html
  • RewriteLog log-path• logs rewrites wel, duh...• relatieve paths zijn relatief ten opzichte van de DocumentRoot• absolute paths zijn ... euhm ... absoluut
  • RewriteLogLevel level• integer 0 - 9• 0 = log niets• 9 = log bijna alles• hoe hoger, hoe meer impact op performantie• 2 of hoger enkel voor debugging
  • GEDAAN• httpd.apache.org/docs/current/mod/mod_rewrite.html• cheat sheet op AddedBytes.com