Examples of URL Rewrite Configuration

The following are examples of URLRewriteConfig settings for different situations that may occur in a web server.

  • Example 1: Basic URL redirection

    The following is an example configuration that matches a URL pattern like 'www.test.com/' and redirects it to 'www.test.com/rewrite.html'.

    # url rewrite config - ex1
    RewriteCond %{HTTP_HOST} ^www\.test\.com$       # if {Host} == "www.test.com"
    RewriteRule ^/$ /rewrite.html [L]               # then "/" > "/rewrite.html"
  • Example 2: Redirecting to an error page if a file does not exist

    The following is an example configuration that will convert a request like 'www.test.com/temp/xxx.html' to 'www.test.com/temp/temp_error.html' when the file 'xxx.html' is not found in the temp directory.

    # url rewrite config - ex2
    RewriteCond %{REQUEST_FILENAME} !-f
    # if {Requested file name} != file or the file pointer
    RewriteRule ^/([^/]+) /$1/$1_error.html [L]
    # then "/temp/xxx.html" > "/temp/temp_error.html"
  • Example 3: Redirecting HTTP requests to HTTPS

    The following is an example configuration that converts a request like 'http://www.test.com:80' to 'https://www.test.com:443'.

    # url rewrite config - ex3
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    # if {Host} == "www.test.com"
    RewriteCond %{SERVER_PORT} 80
    # AND {Port} == "80"
    RewriteRule .* https://www.test.com:443$0 [R]
    # then > "https://www.test.com:443$0"   ($0: request uri)
  • Example 4: Domain change (ignoring URI)

    The following is an example of a configuration that matches a URL pattern such as 'www.test.com/xxx.html' and changes it to 'www.test_new.com/', ignoring the request URI.

    # url rewrite config - ex4
    RewriteCond %{HTTP_HOST} ^www\.test\.com$       # if {Host} == "www.test.com"
    RewriteRule .* http://www.test_new.com [R]       # then > "http://www.test_new.com"
  • Example 5: Domain change (preserving URI)

    The following is an example configuration that matches a URL pattern like 'www.test.com/test.html' and changes it to 'www.test_new.com/test.html', preserving the request URI.

    # url rewrite config - ex5
    RewriteCond %{HTTP_HOST} ^www\.test\.com$
    # if {Host} == "www.test.com"
    RewriteRule .* http://www.test_new.com$0 [R]
    # then > "http://www.test_new.com$0"   ($0: request uri)
  • Example 6: Forbidden error if no Referer header in POST requests

    The following is an example configuration that returns a "403 Forbidden" error when the request method is POST and the Referer header does not exist.

    # url rewrite config - ex6
    RewriteCond %{REQUEST_METHOD} POST              # if {Method} == "POST"
    RewriteCond %{HTTP_REFERER} =""                 # AND {Referer} == ""
    RewriteRule . - [F]                             # then > 403 Forbidden Return
  • Example 7: URL redirection by subdomain

    The following is an example of a configuration that processes "www.test.com/aaa/xxx.html" if the request is "aaa.test.com/xxx.html" and processes "www.test.com/bbb/xxx.html" if the request is "bbb.test.com/xxx.html".

    # url rewrite config - ex8
    RewriteCond %{HTTP_HOST} ^(aaa|bbb)\.test\.com
    # if {Host} == ("aaa.test.com" OR "bbb.test.com")
    RewriteRule .* /%1$0 [L]
    # then "/xxx.html" > "/(aaa|bbb)/xxx.html" (%1: Opening parenthesis of RewriteCond, $0: request uri)
  • Example 8: Changing ports based on query string

    The following is an example of a configuration that rewrites "www.test.com/test.do?query=value1" as "www.test.com:8080/test.do?query=value1" and "www.test.com/test.do?query=value2&.." as "www.test.com:8080/test.do?query=value2&..".

    # url rewrite config - ex9
    RewriteCond %{QUERY_STRING} ^query=value1$ [OR]
    # if {QueryString} == "query=value1"
    RewriteCond %{QUERY_STRING} ^query=value2&
    # OR {QueryString} == "query=value2&.."
    RewriteRule .* http://www.test.com:8080$0 [R]
    # then > "http://www.test.com:8080$0"   ($0: request uri)
  • Example 9: Redirecting requests from specific subdomains to the www domain

    The following is an example a configuration that processes a request to "aaa.test.com/test.html" (not starting with 'www') by redirecting it to "www.test.com/aaa/test.html".

    # url rewrite config - ex10
    RewriteCond %{HTTP_HOST} !^www\.test\.com$
    # if {Host} != www.test.com
    RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9]+)\.test.com$
    # AND {Host} == "(alphanumeric characters).test.com"
    RewriteRule  .* /%1/$0 [L]
    # then > "(alphanumeric string)/$0"   (%1: Opening parenthesis of RewriteCond, $0: request uri)
  • Example 10: Blocking CSS/JS requests without a Referer header

    The following is an example of a configuration that processes requests for CSS or JS files without a Referer header by returning a 403 Forbidden error.

    # url rewrite config - ex11
    RewriteCond %{HTTP_REFERER} !.        # if {HTTP_REFERER} == ""
    RewriteRule \.(css|js)$ - [F]         # then "*.css|*.js" > 403 Forbidden Return
  • Example 11: Converting a specific pattern into a URL query parameter

    The followsig is an example configuration that matches a URL pattern "/user@somehost.com/" and changes it to "/req_test.jsp?blogId=user@somehost.com".

    # url rewrite config - ex12 (REQEUST_URI)
    RewriteCond %{REQUEST_URI} /([a-zA-Z0-9_-]+(@([a-zA-Z0-9_-]+).(com|net|co.kr))?)/?$
    RewriteRule . /req_test.jsp?blogId=%1 [PT,L]
  • Example 12: Redirection based on specific host and URI pattern

    The following is an example of a configuration that redirects requests when the HTTP request header’s host ends with "tmaxsoft.com" and the URL starts with "/redirect/" to "http://www.tmaxsoft.com/redirect.html".

    # url rewrite config - ex13 (HTTP_HOST)
    RewriteCond %{HTTP_HOST} tmaxsoft.com$
    RewriteCond %{REQUEST_URI} /redirect/.*$
    RewriteRule . http://www.tmaxsoft.com/redirect.html [R,L]