4.11 Apache URL Rewriting

The Apache server provides a powerful way to do URL manipulations using Rewrite directives.

For more information refer to Apache HTTP Server Version 2.4: Apache Module mod_rewrite

The following examples illustrate how to use these Rewrite directives.

Example 1

Rewrite URL: "/myservice.jsp?request=xml&service=neworder" to "/cgi-bin/jsmdirect?neworder"

Sample URL: http://10.2.0.170:1099/myservice.jsp?request=xml&service=neworder

If the URL contains a service name value pair then /cgi-bin/jsmdirect?<value> is called else /cgi-bin/jsmdirect?default is call.

 

RewriteCond %{QUERY_STRING} service\=([^\&]+)

RewriteRule ^/myservice.jsp(.*) /cgi-bin/jsmdirect?%1 [L,PT]

RewriteRule ^/myservice.jsp(.*) /cgi-bin/jsmdirect?default [L,PT]

 

The RewriteCond checks the QUERY_STRING for service= and parameterizes any value that is not an & character.

If the condition is true it executes the next RewriteRule else this rule is skipped and the other rule is executed.

Note:

Query string is not available to the RewriteRule directive.

It has already been moved to the query_string environment variable by the time mod_rewrite is activated in a per-directory context.

RewriteCond is only good for the first RewriteRule which follows it.

It does not apply to subsequent RewriteRules, so the %1 backreference becomes undefined for the second, third and other RewriteRules.

Example 2

Rewrite URL: " /myservice.jsp?type=2&msgid=AXD&status=NEW" to "/cgi-bin/jsmdirect?myservice+type(2)msgid(AXD)+status(NEW)"

 

RewriteRule ^/myservice.jsp /myservice.jsp/%{QUERY_STRING} [C]

RewriteRule ^/myservice.jsp/type=([^\&]+)&msgid=([^\&]+)&status=([^\&]+) \

   /cgi-bin/jsmdirect?myservice+type($1)+msgid($2)+status($3) [L,PT]

 

 

Example 3

Rewrite URL: "/parts/00345" to "/cgi-bin/jsmdirect?orderbind+id(00345)"

Sample URL: http://10.2.0.170:1099/parts/00345

 

RewriteRule ^/parts/(.*) /cgi-bin/jsmdirect?orderbind+id($1) [L,PT]

 

Example 4

Rewrite URL: "/parts/00345/abc" to "/cgi-bin/jsmdirect?orderbind+id(00345)+item(abc)"

Sample URL: http://10.2.0.170:1099/parts/00345/abc

 

RewriteRule ^/parts/(.*)/(.*) /cgi-bin/jsmdirect?orderbind+id($1)+item($2) [L,PT]

 

Sample Apache configuration file

 

# Apache Configuration - JSM Services

Options None

Listen 10.2.0.170:1099

ServerRoot /www/jsmapache

DocumentRoot /www/jsmapache/htdocs

# DefaultFsCCSID  37

# DefaultNetCCSID 819

# ServerUserID USERPROFILE

#

LogLevel Warn

LogCycle Daily

ErrorLog logs/error_log

CustomLog logs/access_log combined

LogFormat "%{User-agent}i" agent

LogFormat "%{Referer}i -> %U" referer

LogFormat "%{Cookie}n \"%r\" %t" cookie

LogFormat "%h %l %u %t \"%r\" %>s %b" common

LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

#

SetEnvIf "User-Agent" "Mozilla/2" nokeepalive

SetEnvIf "User-Agent" "JDK/1\.0" force-response-1.0

SetEnvIf "User-Agent" "Java/1\.0" force-response-1.0

SetEnvIf "User-Agent" "RealPlayer 4\.0" force-response-1.0

SetEnvIf "User-Agent" "MSIE 4\.0b2;" nokeepalive

SetEnvIf "User-Agent" "MSIE 4\.0b2;" force-response-1.0

#

# StartCGI 1

# SendBufferSize 32768

# ReceiveBufferSize 32768

#

# Use name-based virtual hosting

NameVirtualHost 10.2.0.170

#

<VirtualHost 10.2.0.170>

  # The first virtual host directive will become the default host

  # This traps the use of the IP address, unsupported or no host name

  # It also has no authority to access the document root directory

  Options None

  ServerName 10.2.0.170

  DocumentRoot /www/jsmapache/htdocs

</VirtualHost>

#

<VirtualHost 10.2.0.170>

  Options None

  ServerName SERVER1

  ServerAlias SERVER1.ACME.COM.AU

  DocumentRoot /www/jsmapache/htdocs-site1

  CGIConvMode BINARY

  ErrorDocument 403 /noaccess.html

  ErrorDocument 404 /notfound.html

  ScriptAliasMatch ^/cgi-bin/jsmproxy(.*) /qsys.lib/devjsm.lib/jsmproxy.pgm$1

  ScriptAliasMatch ^/cgi-bin/jsmdirect(.*) /qsys.lib/devjsm.lib/jsmdirect.pgm$1

  ScriptAliasMatch ^/cgi-sec/jsmdirect(.*) /qsys.lib/secure.lib/jsmdirect.pgm$1

  TimeOut 3000

  #

  RewriteEngine On

  # RewriteLog rewrite-site1.log

  # RewriteLogLevel 9

  #

  RewriteCond %{REQUEST_METHOD} ^TRACE

  RewriteRule .* - [L,F]

  #

  RewriteRule ^/parts/(.*)/(.*) /cgi-sec/jsmdirect?orderbind+id($1)+item($2) [L,PT]

  RewriteRule ^/parts/(.*) /cgi-bin/jsmdirect?orderbind+id($1) [L,PT]

  RewriteRule ^/parts /cgi-bin/jsmdirect?orderbind+id(*NONE) [L,PT]

  #

  RewriteCond %{QUERY_STRING} service\=([^\&]+)

  RewriteRule ^/myservice(.*) /cgi-bin/jsmdirect?%1 [L,PT]

  RewriteRule ^/myservice(.*) /cgi-bin/jsmdirect?default [L,PT]

  #

  RewriteCond %{TIME_HOUR}%{TIME_MIN} >0905

  RewriteCond %{TIME_HOUR}%{TIME_MIN} <1900

  RewriteRule ^/mypage.html /day.html [L]

  RewriteRule ^/mypage.html /night.html [L]

  #

  # RewriteCond %{TIME_HOUR}%{TIME_MIN} <1300 [OR]

  # RewriteCond %{TIME_HOUR}%{TIME_MIN} >1500

  # RewriteRule ^/cgi-bin/jsmdirect(.*) /noaccess.html [L]

  #

  # RewriteMap companymap txt:/www/jsmapache/company.map

  # RewriteRule ^/company/(.*) ${companymap:$1|http://nocompany.com} [L,R]

  #

  # RewriteMap hostmap rnd:/www/jsmapache/randomhost.map

  # RewriteRule ^/(.*\.(pdf|gif|jpg)) http://${hostmap:static}/$1 [L,R]

  #

  <Directory /www/jsmapache/htdocs-site1>

    Options None

    Order Allow,Deny

    Allow from all

    AllowOverride None

  </Directory>

  #

  <Directory /qsys.lib/devjsm.lib>

    Options None

    Order Allow,Deny

    Allow from all

    AllowOverride None

    # Require valid-user

    # AuthType Basic

    # AuthName "Restricted Service"

    # UserID QTMHHTP1

    # PasswdFile %%SYSTEM%%

  </Directory>

  #

  <Directory /qsys.lib/secure.lib>

    Options None

    Order Allow,Deny

    Allow from all

    AllowOverride None

    Require valid-user

    AuthType Basic

    AuthName "Restricted Service"

    # UserID QTMHHTP1

    PasswdFile %%SYSTEM%%

  </Directory>

</VirtualHost>

#

<VirtualHost 10.2.0.170>

  Options None

  ServerName STUDIOADMIN

  DocumentRoot /www/jsmapache/htdocs-site2

  CGIConvMode BINARY

  ScriptAliasMatch ^/cgi-bin/jsmadmin(.*) /qsys.lib/devjsm.lib/jsmadmin.pgm$1

  #

  <Directory /www/jsmapache/htdocs-site2>

    Options None

    Order Allow,Deny

    Allow from all

    AllowOverride None

  </Directory>

  #

  <Directory /qsys.lib/devjsm.lib>

    Options None

    Order Allow,Deny

    Allow from 10.2.1.46

    Allow from 10.2.1.47

    Allow from 10.2.1.48

    AllowOverride None

  </Directory>

</VirtualHost>

#

# Global server directives

#

<Directory />

  Options None

  Order Allow,Deny

  Deny from all

  AllowOverride None

</Directory>

#

<Directory /www/jsmapache/htdocs>

  Options None

  Order Allow,Deny

  Deny from all

  AllowOverride None

</Directory>