Tutorial examples of default syntax policies for rewrite
With the rewrite feature, you can modify any part of an HTTP header, and, for responses, you can modify the HTTP body. You can use this feature to accomplish a number of useful tasks, such as removing unnecessary HTTP headers, masking internal URLs, redirecting Web pages, and redirecting queries or keywords.
In the following examples, you first create a rewrite action and a rewrite policy. Then you bind the policy globally.
This document includes the following details:
- Redirecting an External URL to an Internal URL
- Redirecting a Query
- Rewriting HTTP to HTTPS
- Removing Unwanted Headers
- Reducing Web Server Redirects
- Masking the Server Header
- Converting plain text to URL encoded string and vice-versa
For more information about the commands and syntax descriptions, seeRewrite Command Referencepage.
Redirecting an external URL to an internal URL
这个例子描述了如何创建一个重写ion and rewrite policy that redirects an external URL to an internal URL. You create an action, called act_external_to_internal, that performs the rewrite. Then you create a policy called pol_external_to_internal.
To redirect an external URL to an internal URL by using the command line interface
- To create the rewrite action, at the command prompt, type:
add rewrite action act_external_to_internal REPLACE "http.req.hostname.server" "host_name_of_internal_Web_server"
- To create the rewrite policy, at the Citrix ADC command prompt, type:
add rewrite policy pol_external_to_internal "http.req.hostname.server.eq("host_name_of_external_Web_server") act_external_to_internal"
- Bind the policy globally.
To redirect an external URL to an internal URL by using the configuration utility
- Navigate toAppExpert>Rewrite>Actions.
- In the details pane, clickAdd.
- In theCreate Rewrite Actiondialog box, enter the name act_external_to_internal.
- To replace the HTTP server host name with the internal server name, chooseReplacefrom the Type list box.
- In the header name field, typeHost.
- In the string expression for replacement text field, type the internal host name of your Web server.
- ClickCreateand then clickClose.
- In the navigation pane, clickPolicies.
- In the details pane, clickAdd.
- In the Name field, type pol_external_to_internal. This policy will detect connections to the Web server.
- In the Action drop-down menu, choose the action act_external_to_internal.
- In the Expression editor, construct the following expression:
HTTP.REQ.HOSTNAME.SERVER.EQ("www.example.com")
- Bind your new policy globally.
Redirecting a query
这个例子描述了如何创建一个重写ion and rewrite policy that redirects a query to the proper URL. The example assumes that the request contains a Host header set towww.example.comand a GET method with thestring /query.cgi?server=5. The redirect extracts the domain name from the host header and the number from the query string, and redirects the user’s query to the serverWeb5.example.com, where the rest of the user’s query is processed.
Note: Although the following commands appears on multiple lines, you should enter them on a single line without line breaks.
To redirect a query to the appropriate URL using the command line
- To create a rewrite action named act_redirect_query that replaces the HTTP server host name with the internal server name, type:
add rewrite action act_redirect_query REPLACE q#http.req.header("Host").before_str(".example.com")' '"Web" + http.req.url.query.value("server")#
- To create a rewrite policy named pol_redirect_query, type the following commands at the Citrix ADC command prompt.. This policy detects connections, to the Web server, that contain a query string. Do not apply this policy to connections that do not contain a query string:
add rewrite policy pol_redirect_query q#http.req.header("Host").eq("www.example.com") && http.req.url.contains("?")' act_redirect_query#
- Bind your new policy globally.
Because this rewrite policy is highly specific and should be run before any other rewrite policies, it is advisable to assign it a high priority. If you assign it a priority of 1, it will be evaluated first.
Rewriting HTTP to HTTPS
This example describes how to rewrite Web server responses to find all URLs that begin with the string “http” and replace that string with “https.” You can use this to avoid having to update Web pages after moving a server from HTTP to HTTPS.
To redirect HTTP URLs to HTTPS by using the command line interface
- To create a rewrite action named act_replace_http_with_https that replaces all instances of the string “http” with the string “https,” enter the following command:
add rewrite action act_replace_http_with_https replace_all 'http.res.body(100)' '"https"' -pattern http
- 创建一个名为pol_replace_http_的改写策略with_https that detects connections to the Web server, enter the following command:
add rewrite policy pol_replace_http_with_https TRUE act_replace_http_with_https NOREWRITE
- Bind your new policy globally.
To troubleshoot this rewrite operation, see “Case Study: Rewrite Policy for Converting HTTP Links to HTTPS not Working.”
Removing Unwanted Headers
This example explains how to use a Rewrite policy to remove unwanted headers. Specifically, the example shows how to remove the following headers:
- Accept Encoding header.Removing the Accept Encoding header from HTTP responses prevents compression of the response.
- Content Location header.Removing the Content Location header from HTTP responses prevents your server from providing a hacker with information that might allow a security breach.
To delete headers from HTTP responses, you create a rewrite action and a rewrite policy, and you bind the policy globally.
To create the appropriate Rewrite action by using the command line interface
At the command prompt, type one of the following commands to either remove the Accept Encoding header and prevent response compression or remove the Content Location header:
add rewrite action "act_remove-ae" delete_http_header "Accept-Encoding"
add rewrite action "act_remove-cl" delete_http_header "Content-Location"
To create the appropriate Rewrite policy by using the command line interface
At the command prompt, type one of the following commands to remove either the Accept Encoding header or the Content Location header:
add rewrite policy "pol_remove-ae" true "act_remove-ae"
add rewrite policy "pol_remove-cl" true "act_remove-cl"
To bind the policy globally by using the command line interface
At the command prompt, type one of the following commands, as appropriate, to globally bind the policy that you have created:
bind rewrite global pol_remove_ae 100
bind rewrite global pol_remove_cl 200
Reducing Web Server Redirects
This example explains how to use a Rewrite policy to modify connections to your home page and other URLs that end with a forward slash (/) to the default index page for your server, preventing redirects and reducing load on your server.
To modify directory-level HTTP requests to include the default home page by using the command line
- To create a Rewrite action named action-default-homepage that modifies URLs that end in a forward slash to include the default home page index.html, type:
add rewrite action "action-default-homepage" replace q\#http.req.url.path "/" "/index.html"\#
- To create a Rewrite policy named policy-default-homepage that detects connections to your home page and applies your new action, type:
add rewrite policy "policy-default-homepage" q\#http.req.url.path.EQ("/") "action-default-homepage"\#
- Globally bind your new policy to put it into effect.
Masking the Server Header
This example explains how to use a Rewrite policy to mask the information in the Server header in HTTP responses from your Web server. That header contains information that hackers can use to compromise your Web site. While masking the header will not prevent a skilled hacker from finding out information about your server, it will make hacking your Web server more difficult and encourage hackers to choose less well protected targets.
To mask the Server header in responses from the command line
- To create a Rewrite action named act_mask-server that replaces the contents of the Server header with an uninformative string, type:
add rewrite action "act_mask-server" replace "http.RES.HEADER(\\"Server\\")" "\\"Web Server 1.0\\""
- To create a Rewrite policy named pol_mask-server that detects all connections, type:
add rewrite policy "pol_mask-server" true "act_mask-server"
- Globally bind your new policy to put it into effect.
How to convert plain text to URL encoded string and vice-versa
你下面的表达式转换成纯文本L encoded string and vice-versa:
- URL_RESERVED_CHARS_SAFE (string to URL ENCODED).
Example:
("abc def&123").URL_RESERVED_CHARS_SAFE Output will be “abc%20def%26123” which is url encoded.
- SET_TEXT_MODE(URLENCODED).DECODE_USING_TEXT_MODE. (URL ENCODED to string)
Example:
("abc%20def%26123").SET_TEXT_MODE(URLENCODED).DECODE_USING_TEXT_MODE Output will be “abc def&123”