際際滷

際際滷Share a Scribd company logo
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 畏鍖a乙壊液



   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 畏鍖a乙壊液




   PCRE (Perl-Compatible Regular Expressions)

   vergelijkt met het URL gedeelte (zonder domein)

   RewriteCond nodig om andere stukken te vergelijken
RewriteRule pattern result 畏鍖a乙壊液



kan 辿辿n van de volgende items zijn:

   鍖lesystem path ( /kip/ei.pdf )

   URL path

   volledige URL (met domein)

   -
RewriteRule pattern result 畏鍖a乙壊液




   be誰nvloedt het gedrag van de Rule

   tussen vierkante haakjes [NC]

   meerdere 鍖ags gescheiden door kommas [NC, QSA, L]
RewriteRule pattern result 畏鍖a乙壊液
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteRule pattern result 畏鍖a乙壊液

        C    Chain this rule to the next rule

        E    Set environment variable

         F   403 Forbidden Status

        G    410 Gone Status

        H    Specify handler

        L    Dont process rules after this one

      NC     Case-insensitive

     QSA     Append query string

   R[=301]   Redirect (R=HTTP Status Code)
RewriteCond variable pattern 畏鍖a乙壊液




   HTTP Server Variables (HTTP_HOST, REQUEST_URI)

   pattern wordt vergeleken met de waarde van deze var

   鍖ags die het gedrag van de Condition veranderen
    (net zoals bij een RewriteRule)
RewriteCond variable pattern 畏鍖a乙壊液
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              HTTP_REFERER

               HTTP_HOST

             REMOTE_ADDR

            REQUEST_METHOD

              SERVER_PORT

            HTTP_USER_AGENT

              REQUEST_URI

                  etc.
RewriteCond variable pattern 畏鍖a乙壊液

              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                                   鍖ags
                           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 con鍖g over van de parent

   in virtual server context, neem alle rules over die op
    serverniveau gede鍖nieerd staan

   in directory context, neem alle context, condities en
    rules over die gede鍖nieerd 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

More Related Content

Featured (20)

Artificial Intelligence, Data and Competition SCHREPEL June 2024 OECD dis...
Artificial Intelligence, Data and Competition  SCHREPEL  June 2024 OECD dis...Artificial Intelligence, Data and Competition  SCHREPEL  June 2024 OECD dis...
Artificial Intelligence, Data and Competition SCHREPEL June 2024 OECD dis...
OECD Directorate for Financial and Enterprise Affairs
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
2024 State of Marketing Report by Hubspot
2024 State of Marketing Report  by Hubspot2024 State of Marketing Report  by Hubspot
2024 State of Marketing Report by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
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
Pixeldarts
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
ThinkNow
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
marketingartwork
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
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
Neil 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 2024
Albert 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 Insights
Kurio // 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 2024
Search 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 summary
SpeakerHub
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 Intent
Lily Ray
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
How to Leverage AI to Boost Employee Wellness - Lydia Di Francesco - SocialHR...
SocialHRCamp
2024 State of Marketing Report by Hubspot
2024 State of Marketing Report  by Hubspot2024 State of Marketing Report  by Hubspot
2024 State of Marketing Report by Hubspot
Marius Sescu
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
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
Pixeldarts
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
ThinkNow
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
marketingartwork
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
Neil 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 2024
Albert 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 Insights
Kurio // 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 2024
Search 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 summary
SpeakerHub
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 Intent
Lily Ray
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky

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 畏鍖a乙壊液 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 畏鍖a乙壊液 PCRE (Perl-Compatible Regular Expressions) vergelijkt met het URL gedeelte (zonder domein) RewriteCond nodig om andere stukken te vergelijken
  • 11. RewriteRule pattern result 畏鍖a乙壊液 kan 辿辿n van de volgende items zijn: 鍖lesystem path ( /kip/ei.pdf ) URL path volledige URL (met domein) -
  • 12. RewriteRule pattern result 畏鍖a乙壊液 be誰nvloedt het gedrag van de Rule tussen vierkante haakjes [NC] meerdere 鍖ags gescheiden door kommas [NC, QSA, L]
  • 13. RewriteRule pattern result 畏鍖a乙壊液
  • 14. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 15. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 16. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 17. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 18. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 19. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 20. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 21. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 22. RewriteRule pattern result 畏鍖a乙壊液 C Chain this rule to the next rule E Set environment variable F 403 Forbidden Status G 410 Gone Status H Specify handler L Dont process rules after this one NC Case-insensitive QSA Append query string R[=301] Redirect (R=HTTP Status Code)
  • 23. RewriteCond variable pattern 畏鍖a乙壊液 HTTP Server Variables (HTTP_HOST, REQUEST_URI) pattern wordt vergeleken met de waarde van deze var 鍖ags die het gedrag van de Condition veranderen (net zoals bij een RewriteRule)
  • 24. RewriteCond variable pattern 畏鍖a乙壊液
  • 25. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 26. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 27. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 28. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 29. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 30. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 31. RewriteCond variable pattern 畏鍖a乙壊液 HTTP_REFERER HTTP_HOST REMOTE_ADDR REQUEST_METHOD SERVER_PORT HTTP_USER_AGENT REQUEST_URI etc.
  • 32. RewriteCond variable pattern 畏鍖a乙壊液 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 鍖ags 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 con鍖g over van de parent in virtual server context, neem alle rules over die op serverniveau gede鍖nieerd staan in directory context, neem alle context, condities en rules over die gede鍖nieerd 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

Editor's Notes