SlideShare ist ein Scribd-Unternehmen logo
1 von 46
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 On



Error indien niet beschikbaar
Directives

•   RewriteEngine

•   RewriteCond

•   RewriteRule

•   RewriteOption

•   RewriteLog

•   RewriteBase

•   en meer!
Voorbeeld 1: Fork

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} ^/backend/$
RewriteRule . index.php [NC,L]

RewriteCond   %{REQUEST_URI} !^$
RewriteCond   %{REQUEST_FILENAME} !-f
RewriteCond   %{REQUEST_FILENAME} !-d
RewriteRule   . index.php [NC,L]
Voorbeeld 1: Fork

# activeer Rewriting Engine
RewriteEngine On
# base alle rewrite op de ‘/’ URL
RewriteBase /

# backend is een bestaande directory die behandeld moet worden
# als ieder andere file
RewriteCond %{REQUEST_URI} ^/backend/$
RewriteRule . index.php [NC,L]

# als de URL niet leeg is
RewriteCond %{REQUEST_URI} !^$
# ... en het bestand bestaat niet
RewriteCond %{REQUEST_FILENAME} !-f
# ... en de directory bestaat niet
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite de url naar /index.php
RewriteRule . 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 Engine
RewriteEngine On
# base alle rewrite op de ‘/’ URL
RewriteBase /

# backend is een bestaande directory die behandeld moet worden
# als ieder andere file
RewriteCond %{REQUEST_URI} ^/backend/$
RewriteRule . index.php [NC,L]

# als de URL niet leeg is
RewriteCond %{REQUEST_URI} !^$
# ... en het bestand bestaat niet
RewriteCond %{REQUEST_FILENAME} !-f
# ... en de directory bestaat niet
RewriteCond %{REQUEST_FILENAME} !-d
# rewrite de url naar /index.php
RewriteRule . index.php [NC,L]
2. non-www naar www



RewriteEngine On
RewriteCond %{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 pattern
pattern binnen ronde haakjes                variable ‘$1’
2. non-www naar www



RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]




   vervang door “http://www.example.com”
2. non-www naar www



RewriteEngine On
RewriteCond %{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 www



RewriteEngine On
RewriteCond %{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 sunday



RewriteEngine On
RewriteCond %{TIME_WDAY} 0
RewriteCond %{HTTP_REFERER} (porn) [NC]
RewriteRule .* - [F]
4. test stadium



RewriteEngine On
RewriteCond %{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' directive

RewriteEngine On

#   laat de server weten dat we komen via /xyz en niet
#   via het fysieke path /abc/def

RewriteBase    /xyz

#   the rewriting rules

RewriteRule    ^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

Weitere ähnliche Inhalte

Empfohlen

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Empfohlen (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

mod_rewrite

  • 1. mod_rewrite Bert Pattyn May 2011
  • 2. Wat? • Apache module • Zwitsers mes voor URL manipulatie • op server niveau: httpd.conf • op directory niveau: .htaccess
  • 3. Waarom gebruiken? • Verbergen van de echte URLs • Propere URLs • SEO (oh yeah...) • Redirect naar andere pagina • Toegangscontrole
  • 4. Geïnstalleerd? Zet volgende in .htaccess RewriteEngine On Error indien niet beschikbaar
  • 5. Directives • RewriteEngine • RewriteCond • RewriteRule • RewriteOption • RewriteLog • RewriteBase • en meer!
  • 6. Voorbeeld 1: Fork RewriteEngine On RewriteBase / RewriteCond %{REQUEST_URI} ^/backend/$ RewriteRule . index.php [NC,L] RewriteCond %{REQUEST_URI} !^$ RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . index.php [NC,L]
  • 7. Voorbeeld 1: Fork # activeer Rewriting Engine RewriteEngine On # base alle rewrite op de ‘/’ URL RewriteBase / # backend is een bestaande directory die behandeld moet worden # als ieder andere file RewriteCond %{REQUEST_URI} ^/backend/$ RewriteRule . index.php [NC,L] # als de URL niet leeg is RewriteCond %{REQUEST_URI} !^$ # ... en het bestand bestaat niet RewriteCond %{REQUEST_FILENAME} !-f # ... en de directory bestaat niet RewriteCond %{REQUEST_FILENAME} !-d # rewrite de url naar /index.php RewriteRule . index.php [NC,L]
  • 8. RewriteEngine On • activeert de rewriting engine • verplicht als eerste regel voor alle andere regels
  • 9. 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
  • 10. RewriteRule pattern result [flags] • PCRE (Perl-Compatible Regular Expressions) • vergelijkt met het URL gedeelte (zonder domein) • RewriteCond nodig om andere stukken te vergelijken
  • 11. RewriteRule pattern result [flags] kan één van de volgende items zijn: • filesystem path ( /kip/ei.pdf ) • URL path • volledige URL (met domein) • -
  • 12. RewriteRule pattern result [flags] • beïnvloedt het gedrag van de Rule • tussen vierkante haakjes [NC] • meerdere flags gescheiden door komma’s [NC, QSA, L]
  • 14. 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)
  • 15. 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)
  • 16. 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)
  • 17. 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)
  • 18. 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)
  • 19. 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)
  • 20. 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)
  • 21. 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)
  • 22. 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)
  • 23. 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)
  • 25. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 26. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 27. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 28. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 29. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 30. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 31. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 32. RewriteCond variable pattern [flags] HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 33. 1. Fork (opnieuw) # activeer Rewriting Engine RewriteEngine On # base alle rewrite op de ‘/’ URL RewriteBase / # backend is een bestaande directory die behandeld moet worden # als ieder andere file RewriteCond %{REQUEST_URI} ^/backend/$ RewriteRule . index.php [NC,L] # als de URL niet leeg is RewriteCond %{REQUEST_URI} !^$ # ... en het bestand bestaat niet RewriteCond %{REQUEST_FILENAME} !-f # ... en de directory bestaat niet RewriteCond %{REQUEST_FILENAME} !-d # rewrite de url naar /index.php RewriteRule . index.php [NC,L]
  • 34. 2. non-www naar www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] pattern flags result
  • 35. 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 pattern pattern binnen ronde haakjes variable ‘$1’
  • 36. 2. non-www naar www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] vervang door “http://www.example.com”
  • 37. 2. non-www naar www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] de waarde die binnen de ronde haakjes is opgevangen
  • 38. 2. non-www naar www RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] Redirect met een 301 code Maak dit de laatste RewriteRule
  • 39. 3. no porn guys on sunday RewriteEngine On RewriteCond %{TIME_WDAY} 0 RewriteCond %{HTTP_REFERER} (porn) [NC] RewriteRule .* - [F]
  • 40. 4. test stadium RewriteEngine On RewriteCond %{REMOTE_ADDR} !^8.82.245.147$ RewriteRule .* /temp/index.html [R, NC, L]
  • 41. 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
  • 42. RewriteBase path • zet de basis voor de rewrites
  • 43. 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' directive RewriteEngine On # laat de server weten dat we komen via /xyz en niet # via het fysieke path /abc/def RewriteBase /xyz # the rewriting rules RewriteRule ^oldstuff.html$ newstuff.html # /xyz/oldstuff.html wordt hier dus herschreven naar /abc/def/newstuff.html
  • 44. RewriteLog log-path • logs rewrites wel, duh... • relatieve paths zijn relatief ten opzichte van de DocumentRoot • absolute paths zijn ... euhm ... absoluut
  • 45. 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
  • 46. GEDAAN • httpd.apache.org/docs/current/mod/mod_rewrite.html • cheat sheet op AddedBytes.com

Hinweis der Redaktion

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n
  21. \n
  22. \n
  23. \n
  24. \n
  25. \n
  26. \n
  27. \n
  28. \n
  29. \n
  30. \n
  31. \n
  32. \n
  33. \n
  34. \n
  35. \n
  36. \n
  37. \n
  38. \n
  39. \n
  40. \n
  41. \n
  42. \n
  43. \n
  44. \n