Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
litespeed_wiki:config:internal-redirect [2018/09/27 21:09]
Jackson Zhang [Set header "X-LiteSpeed-Location" in your php script]
litespeed_wiki:config:internal-redirect [2018/09/28 15:42] (current)
Lisa Clarke Proofreading
Line 1: Line 1:
 ====== Internal Redirect ====== ====== Internal Redirect ======
  
-Web server internal redirect via backend response header, aka X-Sendfile or X-Accel-Redirect,​ is a feature used by some web backend developers and popularized by Ruby on Rails. LSWS and OLS use a simple header "​X-LiteSpeed-Location" ​to achieve the same goal.+Web server internal redirect via backend response header, aka ''​X-Sendfile'' ​or ''​X-Accel-Redirect''​, is a feature used by some web backend developers and popularized by Ruby on Rails. LSWS and OLS use a simple header "Location"​ or ''​X-LiteSpeed-Location'' ​to achieve the same goal.
  
-===== What is this internal redirect ​via response header? =====+===== What is This Internal Redirect ​via Response Header? =====
  
-The backend process, instead of returning a full HTTP page response back, return ​only a pointer to a local path.+The backend process, instead of returning a full HTTP page response back, returns ​only a pointer to a local path.
  
-When the web server ​receive ​this special url location pointer via a header variable, the web server will output the content of the specified path, rather than the response from the backend process.+When the web server ​receives ​this special url location pointer via a header variable, the web server will output the content of the specified path, rather than the response from the backend process.
  
-The end user is not aware of this internal redirection and the data returns ​appears from the original url.+The end user is not aware of this internal redirection and the data appears ​to be returned ​from the original url.
  
-===== Implemente internal redirect ​on LiteSpeed ​through header "​X-LiteSpeed-Location" ​=====+===== Implementing Internal Redirect ​on LiteSpeed ​Through the Header ​=====
  
-To get this to work on LiteSpeed, just use a simple ​header "X-LiteSpeed-Location" ​in your php script.+To get this to work on LiteSpeed, just use a simple ​''​X-LiteSpeed-Location''​ header ​in your PHP script.
  
-==== Set header ​"​Location" ​in your php script ​====+==== Method 1: Set "​Location" ​Header ​====
  
-Set "**Location**" ​header pointing to a URL without ''<​nowiki>​http://​domain</​nowiki>''​, just the URL without the hostname ​part. Do not set a "**Status**" ​header in response. Make sure no "Status" ​header is returned. PHP always adds "**Status**" ​header automatically when a "**Location**" ​header ​was set.+In a PHP script, set a ''​Location'' ​header pointing to a URI without ​the ''<​nowiki>​http://​domain.com</​nowiki>''​ part. Do not set a ''​Status'' ​header in response. Make sure no ''​Status'' ​header is returned. PHP always adds a ''​Status'' ​header automatically when a ''​Location'' ​header ​is set.
  
   <?php   <?php
Line 23: Line 23:
   ?>   ?>
  
-==== Set header ​"​X-LiteSpeed-Location" ​in your php script ​==== +==== Method 2: Set "​X-LiteSpeed-Location" ​Header ​==== 
-Add a special ​header "**X-LiteSpeed-Location**" starting from LSWS 3.0.2 to address this, just use it in the same way as a "**Location**" header. For example, ​just put a line like below to the php script:+ 
 +Add a special ​''​X-LiteSpeed-Location''​ header (in LSWS v3.0.2 and above) ​in exactly ​the same way as the ''​Location''​ header was added in **Method 1** above''​X-LiteSpeed-Location''​ is the recommended way to implement an Internal Redirect on LSWS. 
 + 
 +For example, ​add a line like this to the PHP script:
  
   header('​X-LiteSpeed-Location:​ /​path/​to/​file_to_be_redirected'​);​   header('​X-LiteSpeed-Location:​ /​path/​to/​file_to_be_redirected'​);​
  
-**Note:​** ​"/​path/​to/​file_to_be_redirected" ​should be URL without http://​domain but with "/", such as: **test.php** in document root:+**Note:​** ​''​/​path/​to/​file_to_be_redirected'' ​should be a URI without ​''<​nowiki>​http://​domain.com</​nowiki>'', ​but with the preceding ''​/''​, such as can be seen in the ''​test.php''​ script ​in your document root. LSWS only supports "​URI"​ instead of "file path"​. 
   <?php   <?php
   header('​X-LiteSpeed-Location:​ /​img/​php-icon.png'​);​   header('​X-LiteSpeed-Location:​ /​img/​php-icon.png'​);​
   ?>      ?>   
  
-That's it folks. LiteSpeed will take over the the rest, perform an internal redirect, and send back the file with sendfile() support if the url points to a static file.  +That's itfolks. LiteSpeed will take over the rest, perform an internal redirect, and send the file back with ''​sendfile()'' ​support if the URL points to a static file.
  
-Run a test through ​ +Run a test through ​''<​nowiki>​http://​yourdomain.com/​test.php</​nowiki>''​ and you will see it return the ''​php-icon.png''​ image.
-  ​http://​yourdomain.com/​test.php+
  
-It will return the php-icon.png image. +===== Downloading the File Instead ​of Displaying ===== 
- +If you want to download ​the file instead of showing ​it in the browser, you can add an extra header, ''​Content-Disposition'', ​like so:
-==== How to download file instead ​of showing in browser? ​==== +
-If you want to download file instead of showing in browser, you can add extra header ​"Content-Disposition" ​like the following:+
  
   <?php   <?php
Line 47: Line 48:
   header('​X-LiteSpeed-Location:​ /​img/​php-icon.png'​);​   header('​X-LiteSpeed-Location:​ /​img/​php-icon.png'​);​
   ?>      ?>   
-then run http://​yourdomain.com/​test.php will download php-icon.png instead of showing in browser. 
  
-==== LiteSpeed uses a "URL" instead of "file path" ​====+Save the above to ''<​nowiki>​http://​yourdomain.com/​test.php</​nowiki>''​ and run it. The script will download ''​php-icon.png''​ instead of displaying it in the browser. 
 + 
 +===== Redirecting via URL vs. File Path =====
  
-Unlike X-Sendfile or X-Accel-Redirect ​implementation ​in other web servers, LiteSpeed uses a URL instead of file path for security reasons. In this way, only file under document root of a virtual host or a Context ​can be returned, ​otherwise, it could be a huge security issue if for some reason, either ​tricked ​or intentionally, the script ​sent back a header ​"X-Sendfile: /​../​etc/​./​passwd%00" ​or something like thatuser accounts on your server ​is no longer a secret. 8-)+Unlike ​the ''​X-Sendfile'' ​or ''​X-Accel-Redirect''​ implementations ​in other web servers, LiteSpeed uses a URI instead of file path for security reasons. In this way, only file under the document root of a virtual host or a context ​can be returned. Otherwiseyou could have a huge security issue. Imagine ​iffor some reason, either ​accidentally ​or maliciously, the script ​sends back a header ​''​X-Sendfile: /​../​etc/​./​passwd%00'', ​or something like that. The user accounts on your server ​would no longer ​be a secret!
  
-==== Protecting file from direct access ​====+===== Protection ​from Direct Access =====
  
-If you want to prevent user from access the file directly, just use a hard to guess URL like "/​you_never_know/​where_file_is_stored/​...", or you can use a rewrite rule (in httpd.conf) to deny direct access to the directory holding the files, ​something ​like +If you want to prevent ​user from accessing a file directly, just use a hard-to-guess URI like ''​/​you_never_know/​where_file_is_stored/​...''​. Or you can use a rewrite rule (in ''​httpd.conf''​) to deny direct access to the directory holding the files, like so:
  
   RewriteCond %{ORG_REQ_URI} ^/​blocked/​uri/​   RewriteCond %{ORG_REQ_URI} ^/​blocked/​uri/​
   RewriteRule ^/​blocked/​uri/​ - [R=403,F]   RewriteRule ^/​blocked/​uri/​ - [R=403,F]
  
-Here is a version in .htaccess (notice the difference between ^/​blocked... and ^blocked...)+Here is a version in ''​.htaccess'' ​(notice the difference between ​''​^/blocked''​... and ''​^blocked''​...)
  
   RewriteCond %{ORG_REQ_URI} ^/​blocked/​uri/​   RewriteCond %{ORG_REQ_URI} ^/​blocked/​uri/​
   RewriteRule ^blocked/​uri/​ - [R=403,F]   RewriteRule ^blocked/​uri/​ - [R=403,F]
  
-%{ORG_REQ_URI} is a LiteSpeed specific rewrite variable, which refers to the URI in the original request header.+''​%{ORG_REQ_URI}'' ​is a LiteSpeed-specific rewrite variable, which refers to the URI in the original request header.
  
-Another advantage of our internal redirect implementation is that it does not limited ​to sending static files, it can be used to pass the request to another script for further processing. ​:-)+Another advantage of our internal redirect implementation is that it does not limit you to sending static files. It can be used to pass the request to another script for further processing.
  
 ===== Ruby-on-Rails===== ===== Ruby-on-Rails=====
  
-short example ​on how to use Internal Redirect for sending files within a RoR Controller. ​ Below is a sendfile function that can be attached to any action.+Here is a short example ​of how to use Internal Redirect for sending files within a RoR Controller. ​ Below is a sendfile function that can be attached to any action.
  
   def sendfile   def sendfile
  • Admin
  • Last modified: 2018/09/27 21:09
  • by Jackson Zhang