Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

URL Mapping, with and without mod_rewrite

8,010 views

Published on

Yeah, we all have to use mod_rewrite. But there are alternatives in many cases. URL mapping is a much bigger picture than just RewriteRule.

Published in: Technology, Design

URL Mapping, with and without mod_rewrite

  1. 1. mod_rewrite and friends URL Mapping Power Tools Rich Bowen - SourceForge
  2. 2. AKA: Oh, no, not yet anothermod_rewrite talk URL Mapping Power Tools Rich Bowen - SourceForge
  3. 3. URL Mapping What Directory (listing) does that URL mean? Redirect Proxy File Error Handler
  4. 4. mod_rewriteGave that talk for the last four PHP|TekconferencesI think half of you attended those talksSo, what can we do thats different?
  5. 5. Although ... If you actually *wanted* a mod_rewrite talk, that can probably be arranged.
  6. 6. Lets start with theinteresting stuffWe can proceed to the boring stuff as wehave time.
  7. 7. <If>New in 2.4This is the reason youve been looking forto upgrade to 2.4
  8. 8. Expressions Function by vestman, on FlickrMathematical statement which is either trueor falseCan consider any variable that interestsyou
  9. 9. <If "%{HTTP_HOST} != www.example.com"> Redirect permanent / http://www.example.com</If> <If> can be used in any context (server, vhost, directory, .htaccess)
  10. 10. Goodbye mod_rewriteDoesnt replace mod_rewrite in *all*contextsBut it does for many of the things youreusing it forAnd its much more readable, usually
  11. 11. So ...
  12. 12. Example: hotlinkingRewriteCond %{HTTP_REFERER} !www.example.com [NC]RewriteRule .(gif|jpg|png)$ - [F,NC]
  13. 13. Becomes: Note: Shorter is not always better. I think this is *clearer*<If "%{HTTP_REFERER} !~ www.example.com"> <If "%{REQUEST_URI} =~ .(gif|jpg|png)$"> Require all denied </If></If>
  14. 14. Also<If "-R 10.1.0.0/16"> # ...</If><ElseIf "-R 10.0.0.0/8"> # ...</ElseIf><Else> # ...</Else> -R is like "%{REMOTE_ADDR} -ipmatch ..."
  15. 15. More laterWe could give examples of this all day, butlets move on to something else ... By Roby Ferrari, on Flickr
  16. 16. FallbackResourceNew as of 2.2.16You all need it
  17. 17. Youve all seen this:# BEGIN WordPress<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]</IfModule># END WordPress
  18. 18. Youve all seen this:# BEGIN WordPress<IfModule mod_rewrite.c> RewriteEngine On This bit says "If mod_rewrite RewriteBase / is enabled", and is RewriteRule ^index.php$ - [L] completely unnecessary. RewriteCond %{REQUEST_FILENAME} !-f Thats probably a rant for RewriteCond %{REQUEST_FILENAME} !-d another time. RewriteRule . /index.php [L]</IfModule># END WordPress
  19. 19. Youve all seen this:# BEGIN WordPress<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond Annoying bit to keep %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d mod_rewrite happy RewriteRule . /index.php [L]</IfModule># END WordPress
  20. 20. Youve all seen this:# BEGIN WordPress<IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d If its already RewriteRule . /index.php [L] index.php, dont bother</IfModule># END WordPress
  21. 21. Youve all seen this:# BEGIN WordPress If its not a file, and its<IfModule mod_rewrite.c> not a directory (ie, a RewriteEngine On valid resource) RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]</IfModule># END WordPress
  22. 22. Youve all seen this:# BEGIN WordPress Send everything else to<IfModule mod_rewrite.c> index.php as a "front RewriteEngine On controller" RewriteBase / RewriteRule ^index.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L]</IfModule># END WordPress
  23. 23. OrFallbackResource /index.php
  24. 24. How it worksFallbackResource /index.php Triggers just before the error handlers, once other options have been checked. This ensures that existing resources (css, js, images, etc) continue to work as desired.
  25. 25. How it worksFallbackResource /index.php Note that thats a URL path, not just a file name. Otherwise it gets grumpy.
  26. 26. REQUEST_URIFallbackResource /index.php
  27. 27. No need to rewriteRewriteRule ^/(.*) /index.php?$1# or something like that ... Instead ... FallbackResource /index.php$args = explode(/, $_SERVER[REQUEST_URI] ); Remember that the first character of a URL path is always "/"
  28. 28. Robert Fornal, on Flickr *MatchDirectives: AliasMatch, RedirectMatch,ProxyPassMatchContainers: DirectoryMatch, LocationMatch,FilesMatch
  29. 29. AliasMAtch# Keep the images separateAliasMatch ^/image/(.*).jpg$ /files/jpg.images/$1.jpgAliasMatch ^/image/(.*).gif$ /files/gif.images/$1.gifAliasMatch ^/image/(.*).png$ /files/png.images/$1.gif
  30. 30. Case insensitive AliasMatch (?i)^/image(.*) /ftp/pub/image$1
  31. 31. Everything but ...RedirectMatch ^/(?!images/)(.*) http://dynamic.myhost.com/$1
  32. 32. Everything but ...RedirectMatch ^/(?!images/)(.*) http://dynamic.myhost.com/$1 This is called a zero-width assertion. Zero-widthbecause it doesnt capture a backreference
  33. 33. Everything but ...RedirectMatch ^/(?!images/)(.*) http://dynamic.myhost.com/$1 Matches anything thatDOESNT start with images/
  34. 34. Everything but ...RedirectMatch ^/(?!images/)(.*) http://dynamic.myhost.com/$1Result: Everything thats not an image goes over *there*
  35. 35. FilesMatch Recommended way to configure .php files with mod_php<FilesMatch .php$> SetHandler application/x-httpd-php</FilesMatch>
  36. 36. FilesMatch Recommended way to configure .php files with mod_php<FilesMatch .php$> SetHandler application/x-httpd-php</FilesMatch> Avoids .php.txt files being processed, for example
  37. 37. ProxyPassMatchProxyPassMatch ^/(.*.gif)$ http://backend.example.com:8000/$1
  38. 38. ProxyPassMatch ProxyPassMatch ^/(.*.gif)$ http://backend.example.com:8000/$1 Security note: The target URLshouldnt be modifiable by the URL. That is, there must not be any way that a cleverlycrafted URL could result in the target URL being something nefarious.
  39. 39. Consider:ProxyPassMatch ^/(.*)$ http://backend.example.com$1 What if my URL was http://yourserver.com/.myserver.com/
  40. 40. Consider:ProxyPassMatch ^/(.*)$ http://backend.example.com$1 What if my URL was http://yourserver.com/.myserver.com/
  41. 41. Consider:ProxyPassMatch ^/(.*)$ http://backend.example.com$1 Gotcha What if my URL washttp://backend.example.com.myserver.com/http://yourserver.com/.myserver.com/index.php
  42. 42. Consider:ProxyPassMatch ^/(.*)$ http://backend.example.com$1 Fortunately, this actually shouldnt work, since ProxyPassMatch will return a 500 if the target URL isnt valid before the substitution. (ie, no trailing slash)
  43. 43. Oh, all rightWell do some mod_rewrite stuff, becauseyoud go home sad if I didnt.
  44. 44. Did you know ...You can now use expr in RewriteCond, whichis practically magic
  45. 45. RewriteCond expr "! %{HTTP_REFERER} -strmatch *://%{HTTP_HOST}/*"RewriteRule ^/images - [F]
  46. 46. New keyword exprRewriteCond expr "! %{HTTP_REFERER} -strmatch *://%{HTTP_HOST}/*"RewriteRule ^/images - [F]
  47. 47. -strmatch is a glob, not a regexRewriteCond expr "! %{HTTP_REFERER} -strmatch *://%{HTTP_HOST}/*"RewriteRule ^/images - [F]
  48. 48. RewriteCond expr "! %{HTTP_REFERER} -strmatch *://%{HTTP_HOST}/*"RewriteRule ^/images - [F] If the referer doesnt look like the source ...
  49. 49. RewriteCond expr "! %{HTTP_REFERER} -strmatch *://%{HTTP_HOST}/*"RewriteRule ^/images - [F] Yes, this is yet another way to prevent "hot linking" of images.
  50. 50. RewriteMapTable-based lookupMore programmatic lookupsDatabase queries
  51. 51. Database rewrites RewriteMap supports database queries in 2.4RewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  52. 52. Map NameRewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  53. 53. ArgumentRewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  54. 54. DefaultRewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  55. 55. Query is prepared and executed, so sql injection is mitigated, but you still need to be careful.RewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  56. 56. Use "fastdbd" to enable query caching, "dbd" to do without cachingRewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  57. 57. Default value if theres no returned value. If multiple values, one is selected randomlyRewriteMap myquery "fastdbd:SELECT ID FROM breeds WHERE name = %s"RewriteRule ^/dogs/(.*) /breeds.php?id=${myquery:$1|0} [PT]
  58. 58. mod_proxy_htmlNew module in 2.4Was available as third-party module inearlier versionsSimplifies proxying to a back-end app
  59. 59. mod_proxy_htmlProxying to internal server which isclosed-source, or that you dont have timeto monkey withGenerated HTML has fully-qualified URLs:
  60. 60. Clienthttp://app.local/ http://example.com/ <html> <a href="http:// app.local"> Link</a> ...
  61. 61. mod_proxy_htmlFixes up the HTMLAlso inspects cookies, and other headers,and fixes those
  62. 62. mod_proxy_expressAnother new module in 2.4Simplifies the creation of proxying a bunchof vhosts to back-end servers
  63. 63. ####express-map.txt:##www1.example.com http://192.168.211.2:8080www2.example.com http://192.168.211.12:8088www3.example.com http://192.168.212.10
  64. 64. ####express-map.txt:##www1.example.com http://192.168.211.2:8080www2.example.com http://192.168.211.12:8088www3.example.com http://192.168.212.10 httxt2dbm -i express-map.txt -o emap ProxyExpressDBMFile emap
  65. 65. mod_proxy_expressEspecially awesome for a bunch of VMs,with a front-end proxy/cache serverAvoids having a jillion ProxyPass directivesor rewrite rules
  66. 66. And while were onthe subjectmod_proxy_balancer is twice as caffeinatedas it used to be
  67. 67. mod_proxy_balancer<Proxy balancer://mycluster> BalancerMember http://192.168.1.50:80 BalancerMember http://192.168.1.51:80</Proxy>ProxyPass /test balancer://myclusterProxyPassReverse /test balancer://mycluster
  68. 68. Balance By ...Busyness SuperFantastic, on FlickrByrequestsBytrafficheartbeats
  69. 69. Heartbeats Mark McLaughlin, on FlickrThis is newmod_heartmonitor and mod_heartbeatverify that a server is alive, and now youbalance by what server is more idle
  70. 70. Finrbowen@geek.net@rbowen https://joind.in/6509

×