CGI: URL rewriting

From The Uniform Server Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

URL rewriting

Web pages that have a file extension of .vbs or .wsf expose the underlying technology used. These file extensions apart from being alien to most Internet users are a potential security risk.

This page covers url rewriting using the .htaccess file to change file extensions .vbs or .wsf to .htm on a per directory basis.


URL rewriting requirements

To rewrite a URL you must have the Apache mod_rewrite module loaded. The Apache directive FollowSymLinks must be used otherwise a 500 Internal Sever Error will be generated

Note: If you use the root folder (www) all pages on your server with extension .htm will be rewritten. This probably is not desirable especially if you use genuine htm pages. It is better to use a sub-folder and locate your dynamic pages in this along with the .htaccess file.

URL rewriting example

Apache’s rewrite engine allows you to change the file extension of any file.

The following example will rewrite some_page.htm to some_page.php

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]
  • User types http://localhost/test.htm into browser address bar.
  • The page request is sent to your Apache server.
  • Apache detects the .htm extension and replaces it with .php
  • Apache then locates the .php page and servers it to the browser.


This slight of hand is achieved by the RewriteRule which performs the following:

  • Search this URL for the following pattern (.*)\.htm Thats any characters (.*) plus the extension .htm
    • Note 1: The carrot ^ means start at the beginning of the line and continue looking for a match until the end of line anchor $ is reached.
    • Note 2: Anything that matches the pattern contained in brackets (.*) is to be stored in variable $1
  • Substitute pattern in the original URL (.*)\.htm with the new pattern $1.php
  • Redirect internally to the new URL created.
  • Ensure the search is case insensitive [nc]


Similarly the following will rewrite some_page.htm to some_page.vbs

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.vbs [nc]

And this will rewrite some_page.htm to some_page.wsf

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.swf [nc]

Top

Test script 19 - URL rewrite 1

This test script demonstrates rewriting files with extension htm to internal files with extension wsf.

Modify .htaccess file

For Apache to use our rewrite rule the rewrite engine needs to be enabled using the directive
RewriteEngine on for this we use the .htaacess file. This is followed by any rewrite rules as required.


Edit .htaccess:
Edit the .htaccess file in folder vbs_test to have content as shown on the right.

#Enable CGI scripts
AddHandler cgi-script .vbs .wsf
Options +ExecCGI
Options +FollowSymlinks

#Remap received htm files to internal wsf files 
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.wsf [nc]

Test script 19

The script is self-explanatory however there are some points worthy of note.

  • All links including the form use page url url_1.htm.
  • The internal page on the server is named url_1.wsf

Run script

  • Create a new file url_1.wsf with content as shown on right.
  • Save to test folder \www\vbs_test
  • Start Apache if not already running
  • Enter: http://localhost:8081/vbs_test/url_1.htm into browser.

Result:
Click the "Get" button and link Link1.
Note the address displayed in the browser address bar.

The page is displayed and the address bar shows the .htm extension.

'!c:/windows/system32/cscript //nologo

<job>
'-- Include any external files
<script language="VBScript" src="common_functions.vbs"/>

' Main script ---
<script language="VBScript">
Option Explicit ' Force explicit variable declaration.
Wscript.Echo "Content-type: text/html"&vbLF&vbLF
Wscript.Echo "<title>Test 19</title>"

Dim cgi
set cgi  = New cgi_get_var_class     'Create new object

'Display form variables
Wscript.Echo  "Text box var  = " & cgi.getvar("url_test")& "<br />"
Wscript.Echo  "Link1 value   = " & cgi.getvar("link1")& "<br /><br />"

'--Form method get
WScript.Echo  "<form method=""GET"" action=""url_1.htm"" >"
WScript.Echo  "  <input type=""text"" name=""url_test"" value=""Get Test"">"
WScript.Echo  "  <input type=""submit"" value=""Get"">"
WScript.Echo  "</form>"
WScript.Echo  "<a href=""url_1.htm?link1=link1"">Link1</a> Calls page url_1.htm <br />"
</script>
</job>

Top

Summary

That concludes this tutorial the code snippets covered should help you to quickly start scripting dynamic webs pages using either VBScript or Javascript.

Top