Post on 22-Nov-2014
description
How to redirect URLHow to redirect URLHow to redirect URLHow to redirect URLwith Apache web server with Apache web server
1Informational 2 Success 3 Redirection 4 Client error 5Server error
xx1 xx2 xx3 xx4 xx5
Topics1 2
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL• Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
Http status codeIt i t t d hi h i t f th HTTP/ t d d It is response status code which is part of the HTTP/1.1 standard.
1 f l i f i
xx1Informational Server is performing some further action and does not yet have a definitive response
4 Client error
2xx4 Error was found from
client side
xx2 SuccessThe action requested by the client was successful received
5Server error
xx5 The server failed to fulfill a valid request
xx3 RedirectionSite will be redirected or URL forwarding
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL• Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
xx1Informational Server is performing some further action and does not yet have a d fi i i
1 2
xx1 definitive response
Status code DescriptionStatus code Description
100 continue
Allow a client that is sending a request message to determine if server is willing to accept the request.
101 Switching Protocols
Server will switch protocols to those defined by the response's Upgrade header field immediately after the empty line which terminates the 101 response.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL• Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
xx2 SuccessThe action requested by the client was successful received
1 2
Status code Description
xx2 successful received
Status code Description
201 created New resource being created.
202 accepted The request has been accepted for processing.
203 203 non-
authoritative information
The returned metainformation in the entity-header is not the definitive set as available from the origin server.
204 no content
The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.
205 The server has fulfilled the request and the user agent should reset the document view which reset content caused the request to be sent.
206 partial content The server has fulfilled the partial GET request for the resource.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL• Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
xx3 RedirectionSite will be redirected or URL forwarding
1 2
Status code Description
xx3Status code Description
300 Multiple Choices The requested resource correspond to multiple specific location.
301Moved
PermanentlyThe requested resource has been assigned to new permanent URI.
302 Found
The requested resource resides temporarily under a different URI. This response is only cacheable if indicated by a Cache-Control or Expires header field.
303 S Oth
The response to the request can be found under a different URI and SHOULD be retrieved using a GET th d th t See Other GET method on that resource.
304 Not Modified
If the client has performed a conditional GET request and access is allowed, but the document has not been modified.
305 Use Proxy The requested resource must be accessed through the proxy given by the location field.
Status code Description
1 2
Status code Description
306 (Unused)
The 306 status code was used in a previous version of the specification, is no longer used, and the code is reserved.
307 Temporary
Redirect
If the 307 status code is received in response to a request other than GET or HEAD, the user agent must not automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL• Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
xx4 Client errorError was found from client side
1 2
xx4Status code DescriptionStatus code Description
400Bad Request The requested cannot be fulfilled due to bad syntax.
401Unauthorized It will happen when authentication has failed.
402 Payment Required
Reserve for future use for digital cash or micro payment.
403 Th q t l l q t b t th i f i t d t it4 3Forbidden The request was a legal request, but the server is refusing to respond to it.
404 Not Found
The requested resource could not be found but may be available again in the future.
405 Method Not
Allowed
A request was made of a resource using a request method not supported by that resource. For example, using GET on a form which requires data to be presented via POST.
Status code Description
1 2
406 Not Acceptable
The requested resource is only capable of generating content not acceptable according to the Accept headers sent in the request.
407 Proxy
Authentication Required
It need proxy authentication for accessing the site.
408 Request Timeout The server timed out waiting for the request.
409 Indicates that the request could not be processed because of conflict in the request4 9Conflict Indicates that the request could not be processed because of conflict in the request.
410 Gone Indicates that the resource requested is no longer available and will not be available again.
411 Length Required The request did not specify the length of its content.
412 Precondition
FailedThe server does not meet one of the preconditions that the requester put on the request.
413 Request Entity
Too LargeThe request is larger than the server is willing or able to process.
Status code Description
1 2
414 Request-URI
Too LongThe URI provided was too long for the server to process.
415 Unsupported Media Type
The request entity has a media type which the server or resource does not support.
416 416 Requested Range
Not SatisfiableThe client has asked for a portion of the file, but the server cannot supply that portion.
417 E t ti Th t t th i t f th E t t h d fi ldExpectation
FailedThe server cannot meet the requirements of the Expect request-header field.
418 I'm a teapot This request is not expected to be implemented by actual HTTP servers.p
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL• Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
xx5Server errorThe server failed to fulfill a valid request
1 2
xx5Status code DescriptionStatus code Description
500 Internal Server
ErrorA generic error message, given when no more specific message is suitable.
501 Not
ImplementedThe server either does not recognize the request method, or it lacks the ability to fulfill the request.
502 Bad Gateway
The server was acting as a gateway or proxy and received an invalid response from the upstream server.
503 S i Th i tl il bl (b it i l d d d f i t )Service
UnavailableThe server is currently unavailable (because it is overloaded or down for maintenance).
504 Gateway Timeout
The server was acting as a gateway or proxy and did not receive a timely response from the upstream server.Timeout
505 HTTP Version Not Supported
The server does not support the HTTP protocol version used in the request.
Status code Description
1 2
p
506Variant Also Negotiates
Transparent content negotiation for the request results in a circular reference.
507Insufficient
StorageDisk is running out of space.
509509Bandwidth Limit
ExceededThis status code, while used by many servers, is not specified in any RFCs.
510510Not Extended Further extensions to the request are required for the server to fulfill it.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL• Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
Main reason for redirect URL1 2
1. SecurityHide underlying technology – index.php
y g gy p pHide actual path of files – http://www.xxx.com/images/simple?photo=34
2. Good for peopleEasy to remember – http://www.xxx.com
3. Good for search engineSearch engine like friendly URL – http://www.xxx.com/contact
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL Preparing stuff
2
p g• Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
Preparing stuff1 2
Install Apache on your web server and enable “mod_rewrite” engine
Rewrite Engine OnRewrite Engine On
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL Preparing stuff
2
p g Where we place the rule• Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
Where we place the rule1 2
• Apache config file - httpd.conf
• Htaccess file - .htaccess
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL Preparing stuff
2
p g Where we place the rule Rewrite rule• Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
Rewrite rule1 2
To be more understanding for rewrite rule, It’s good to familiar with the syntax andpattern of rewrite rule.
RewriteRule ^somefile-([0-9]+)\.htm$ /dir/index.php?id=$1 [L]
Pattern Target Flag
p p
Pattern1 2
Metacharacter
Pattern has been used for matches the URL path and Regular expression has been used to detect the requested URI.
Char Definition
\ Use before any of the following characters to escape or null the meaning or it \* \ \$ \+ \[ \]
Metacharacter
\ Use before any of the following characters to escape or null the meaning or it. \ \. \$ \+ \[ \]
^ Start matching at this point.
$ End point of the match.
. Any character.
[] Starts a class.
| Starts alternative match this|that would mean match this or that.| Sta ts a te at ve atc t s|t at wou d ea atc t s o t at.
() Starts a back reference point.
? Match 0 or 1 time Quantifier.
+ Match atleast 1 or more times Quantifier.
* Match 0 to infinite times Quantifier.
{} Match minimum to maximum Quantifier {0,3} match up to 3 times.
Class []1 2
Char Definition
^ Negates the class. [^A-Z]+ means don't match any uppercases.
\ Use before any of the following characters to escape or null the meaning or it. [\+]+.\ y g p g [\ ]
- Range for matching [0-9]+ [a-zA-Z]+.
Targetb d f l i h d
1 2
It can be used for replacing any matched URL.
File-system path
URL pathcould be used
Absolute URL as a target.
- [dash] (no substitution)
Flag1 2
Char Definition
d dd h h
Flag can modify the behavior of a Rewrite rule.
[R] Redirect you can add an =301 or =302 to change the type.
[F] Forces the URL to be forbidden. 403 header.
[G] Forces the URL to be gone 401 header.
[L] Last rule. (You should use this on all your rules that don't link together)
[N] Next round. Rerun the rules again from the start.
[C] Chains a rewrite rule together with the next rule[C] Chains a rewrite rule together with the next rule.
[T] Use T = MIME-type to force the file to be a mime type.
[NS] Use if no sub request is requested.
[NC] Makes the rule case Insensitive.
[QSA] Query String Append use to add to an existing query string.
[NE] Turns of normal escapes that are default in the rewrite rule.[NE] Turns of normal escapes that are default in the rewrite rule.
[PT] Pass through to the handler. (together with mod alias)
[S] Skip the next rule S=3 skips the next 3 rules.
[E] E = sets an environmental variable that can be called by other rules.
[P] E = rules are served through a proxy subrequest.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL Preparing stuff
2
p g Where we place the rule Rewrite rule Example 1 : non-www to www redirection• Example 2 : normal redirection• Example 2 : normal redirection• Example 3 : sub domain redirection
Example 1 : non-www to www redirection h d h
1 2
M h h “h //
http://xxx.com => redirect to http://www.xxx.com
RewriteEngine On
Match this URL “http://xxx.com”
RewriteCond %{HTTP_HOST} ^xxx\.com
The pattern captured inside the parentheses
RewriteRule ^(.*)$ http://www.xxx.com/$1 [R=301,L]
Pattern inside parentheses is Replace with this URL Redirect with 301 andPattern inside parentheses is capture as variable “$1”
Replace with this URL Redirect with 301 and make this as the last Rewrite rule.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL Preparing stuff
2
p g Where we place the rule Rewrite rule Example 1 : non-www to www redirection Example 2 : normal redirection Example 2 : normal redirection• Example 3 : sub domain redirection
Example 2 : normal redirectionh d h b
1 2
M h h “h //
http://xxx.com => redirect to http://yyy.com/about
RewriteEngine On
Match this URL “http://xxx.com”
RewriteCond %{HTTP_HOST} ^xxx\.com
RewriteRule ^/(.*)$ http://yyy.com/about [R=301,QSA,L]
Pattern inside parentheses is Replace with this URL Redirect with 301Pattern inside parentheses is capture as variable “$1” but it is not used for this case
Replace with this URL Redirect with 301, query append string and make this as the last rewrite rule.
Topics
LEARN ABOUT HTTP STATUS CODE
Http status code
1
Http status code 1xx Informational 2xx Success 3xx Redirection 4xx Client error 5xx Server error
HOW TO REDIRECT YOUR URL Main reason for redirect URL Preparing stuff
2
p g Where we place the rule Rewrite rule Example 1 : non-www to www redirection Example 2 : normal redirection Example 2 : normal redirection• Example 3 : sub domain redirection
Example 3 : sub domain redirectionh bl d h bl
1 2
http://xxx.com/blog/ => redirect to http://blog.xxx.com/
RewriteEngine On
RewriteRule ^/blog/?(.*)$ http://blog.xxx.com/$1 [P,NC,L]
M h “/bl /” R l i h hi URL R l dMatch “/blog/” pattern Replace with this URL Rules are served through proxy, case insensitive and make this as the last Rewritethis as the last Rewrite rule.
References
http://www.webforgers.net/mod-rewrite/mod-rewrite-syntax.phphttp://httpd.apache.org/docs/1.3/mod/mod_rewrite.htmlhttp://www.w3.org/Protocols/rfc2616/rfc2616-sec10.htmlhttp://en.wikipedia.org/wiki/List_of_HTTP_status_codeshttp://en.wikipedia.org/wiki/URL_redirection#Manual_redirect
Thank you for watchingy gmy presentation
Created by
panu ausavasereelert
blog: http://panu.in.th
email: panu@panu in themail: panu@panu.in.th
twitter: @panuinth