part 5 apache MOD REWRITE

url rewrite with mod_rewrite.so  engine#
 
feature:
1)changes url or underlying request based on condition
2)based on perl regex
  important characters are
  . = matches a  single character
  * = matches zero or more of previous character
  + = matches one or more
  ? = optional match
  [] = character class  i,e '/prod[123]*'
  [^] = character class negation
  () = groups character for assignation to backreference , eg '/(prod|products)'
3) ability to parse:
  HTTP headers
  Server VARs - %{HTTP_HOST}
  enviromental VARs i,e IE
 
 http://hostname/URI
 HTTP= scheme
 host=hostname
 urlpath = /.. = basic rewrites operate here

  •        <Directory>
  •        .htaccess
      
      
RewirteRule - basic requirement - Takes 3 argument
usage: 'RewriteRule Pattern(URL-Path) Substitution [flag[s]]
Note: URL-Path matches after HOST and before Query String
eg http://site2.example.com/prod/index.php?pid=1&user=current

Note: substitution can be:
    a) File system path(mod_alias):
    RewriteRule ^/docs /usr/local/share/apache2/docs
    Note: The above uses 'mod_rewrite' as mod_alias
   
    b)web path:
    RewirteRule ^/docs$ /documents
    note: '/documents' exists beneath the DocumentRoot
   
    c)absolute URL: 'RewirteRule ^/docs$ http://www.testing.com/docs [R]'
    rewrites users browser URL to reflect new absolute URL
   
    Redirect
    No Change: '-'
    RewriteRule \.(jpg|png) - [E=image:1]  =>this enable VAR for later usage
Note Flags are optional can be multiple [f1,f2,f3...] and include common option
[NC] = case insensitive
[E=Var:Value] - setting of enviroment VAR -useful in logs,other rewrite rule, CGI
[F] = Returns 403 to client forbidding access to resource
[G] = Returns 410 to client resource is no longer available
[R] = returns redirect 300-399 to client

note: consult mod_rewrite docs for more flags

======================================================================

ensure module is loaded

ln -s . /etc/apache2/mods-available/rewrite.loaded

vim 000-default.conf
<virtualhost>
..
#request for example.com/index.html is sent to example.com/index2.html
RewriteEngine on   #enables processing
RewriteRule ^/index.html$ /index2.html #fetches new content but does not Redirect BROWSER (eg. example.com/index.html will serve content from index2.html but BROWSER url bar is not changed )
RewriteRule ^/index.html$ /index2.html [R] #fetches new content ALSO Redirect bROWSER #url is changed in browser
RewriteRule ^/docs$ http://testing.com/docs [R]


</virtualhost>


#mod rewrite - rewrite condition
 

features:

 stackability of logic
 rules are implicitly ANDed: use [OR] to enable ORing
 one or more tests may be performed on current request
 Supports backreferences using: %N - %1..%9
 Usage 'RewriteCond TestString CondPattern (RegEx) [OR, ornext, NC,NV
   teststring:
      RewriteRule
     
Task:
omit localhost (127.0.x.y) from apache logs
 a)  RewriteCond %{REMOTE_ADDR} ^127\.0\.
 b)  RewriteRule .* - [E=localnolog] => Defines VAR to indicate request from localhost
 c)  'CustomLog /var/log/apache/access.log combined env=!localnolog'



 Force SSL for url path '/login$'

 'RewriteCond %{REQUEST_URI} ^/login'
 'RewriteCond ${SERVER_PORT} !443'
 'RewriteRule .* https://%{HTTP_HOST}/login'
OR
 RewriteCond %{REQUEST_URI} ^(/login.*)
RewriteCond %{SERVER_PORT} !443
RewriteRule .* https://%{HTTP_HOST}/login.html

Force ssl for all
RewriteCond %{SERVER_PORT} !443
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI

RewriteEngine on
RewriteCond %{HTTP_HOST} ^lokeshworhandicraft.com.np [NC,OR]
RewriteCond %{HTTP_HOST} ^www.lokeshworhandicraft.com.np [NC]
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule ^(.*)$ /public/$1 [L]

 

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}





Back reference

##mod rewrite##
Backreferences

feature
 #tokenize portion of URL for reuse
 #token range form $1 .. $9
 #uses '()' to create token
 #per server and per directory (.htaccess | <Directory>)
 Note: normally placed at site-wide or virtualHost layer
 Note: normally used to match: URL-Path

 task:
 rewrite index.html -> /new/index.html
 ------------------------------------------
 RewriteRule ^/(index.html)$ /new/$1  #equivalent to redirect[Match]
 RewriteRule ^/(index.html)$ /new/$1 [R] #equivalent to Redirect[Match] - Force redirect


 Rewrite 'localhost/docs*' -> 'http://www.facebook.com/docs*
 a. RewriteRule ^(/docs.*)$ http://linuxorg.com$1 - Redirects: '/docs* to remote server










No comments:

Post a Comment