Apache

 
 
Apache is a powerful and flexible HTTP/1.1 compliant web server. Originally designed as a replacement for the NCSA HTTP Server, it has grown to be the most popular web server on the Internet. As a project of the Apache Software Foundation, the developers aim to collaboratively develop and maintain a robust, commercial-grade, standards-based server with freely available source code.

The Apache HTTP Server is distributed at no charge for commercial or non-commercial use. For more information read the LICENSE.txt file.

Apache can be used with Microsoft Windows. The Apache HTTP Server Version 2.0 runs under Windows NT, Windows 2000 and Windows XP. You should download the version of Apache for Windows with the .msi extension. This is a single Microsoft Installer file containing Apache, ready to install and run.

The latest Apache HTTP Server version can be downloaded from: http://httpd.apache.org/
The latest Apache 1.3 documentation can be found at: http://httpd.apache.org/docs/1.3/
The latest Apache 2.0 documentation can be found at: http://httpd.apache.org/docs/2.0/
The latest Apache 2.2 documentation can be found at: http://httpd.apache.org/docs/2.2/







Apache rewrite.



Information
The mod_rewrite module is a rule-based rewriting engine (based on a regular-expression parser) to rewrite requested URLs on the fly, for example redirecting an url to a new url without changing the actual filename and path.

This module operates on the full URLs and can be set in the: More information can be found at: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Operating system used
Windows XP Home Edition Version 5.1 SP 2

Software prerequisites
Apache 2.0.49

Procedure

Rewrite using the httpd.conf file
  1. Open the C:\Tools\Apache Group\Apache2\conf\httpd.conf file and enable the mod_rewrite module by uncommenting line:
    LoadModule rewrite_module modules/mod_rewrite.so

  2. The rewrite rule can be applied in the:

  3. In this step the location of each context is displayed within the httpd.conf file, including a few simple rewrite directives.



    SERVER CONFIG CONTEXT

    Listen 80

    ServerAdmin [email protected]

    ServerName www.mobilefish.com:80

    DirectoryIndex index.html index.html.var index.shtm index.htm

    RewriteEngine On
    RewriteLogLevel 1
    RewriteLog "logs/www.mobilefish.com-rewrite_log"
    RewriteRule ^(.*)/oldurl\.html$     $1/newurl.html    [L]




    VIRTUAL HOST CONTEXT

    Listen 80

    ServerAdmin [email protected]

    ServerName www.mobilefish.com:80

    DirectoryIndex index.html index.html.var index.shtm index.htm

    NameVirtualHost 192.168.1.100

    <VirtualHost 192.168.1.100:80>
       ServerAdmin webmaster@mobilefish.com
       DocumentRoot c:/mobilefish_web
       ServerName www.mobilefish.com
       ErrorLog logs/www.mobilefish.com-error_log
       #TransferLog logs/www.mobilefish.com-access_log
       CustomLog "|c:/weblogs/rotatelogs.exe
          c:/weblogs/www.mobilefish.com-access_%y%m%d.log 86400" combined
       # The rewrite rule applies to this virtual server.
       # The rule pattern get applied to the full URL and
       # the substitution have to create full URL.
        RewriteEngine On
        RewriteLogLevel 1
        RewriteLog "logs/www.mobilefish.com-rewrite_log"
        RewriteRule ^(.*)/oldurl\.html$    $1/newurl.html    [L]
       <Directory "c:/mobilefish_web">
           Options +Includes
           AddType text/html shtm
           AddOutputFilter Includes shtm
       </Directory>
    </VirtualHost>



    PER-DIRECTORY CONFIG CONTEXT

    Listen 80

    ServerAdmin [email protected]

    ServerName www.mobilefish.com:80

    DirectoryIndex index.html index.html.var index.shtm index.htm

    NameVirtualHost 192.168.1.100

    <VirtualHost 192.168.1.100:80>
       ServerAdmin webmaster@mobilefish.com
       DocumentRoot c:/mobilefish_web
       ServerName www.mobilefish.com
       ErrorLog logs/www.mobilefish.com-error_log
       #TransferLog logs/www.mobilefish.com-access_log
       CustomLog "|c:/weblogs/rotatelogs.exe
          c:/weblogs/www.mobilefish.com-access_%y%m%d.log 86400" combined
       <Directory "c:/mobilefish_web">
           Options +Includes
           AddType text/html shtm
           AddOutputFilter Includes shtm
       </Directory>
      <Directory "c:/mobilefish_web/download/apache/demo">
           # The rewrite rule only applies to this specific directory.
           RewriteEngine On
           RewriteRule ^oldurl\.html$     newurl.html    [L]
      </Directory>
    </VirtualHost>

    Get an updated httpd.conf file where the above mentioned rewrite rule is placed inside the per-directory config context.



    If you want to test:

    RewriteRule ^oldurl\.html$     newurl.html    [L]

    do the following:

    • Make the changes in the httpd.conf file and restart Apache.
    • Create a demo subdirectory somewhere in your docroot.
      For example: C:\mobilefish_web\download\apache\demo

    • Create the following two files and put them in the demo directory:

      File oldurl.html:
      <html>
      <head>
          <title>Old URL</title>
      </head>
      <body>
          <h1>Rewrite is not working</h1>
      </body>
      </html>

      File newurl.html:
      <html>
      <head>
          <title>New URL</title>
      </head>
      <body>
          <h1>Rewrite is working</h1>
      </body>
      </html>


    • In a browser type:
      https://www.mobilefish.com/ download/apache/demo/oldurl.html

      If the rewrite is working, you should see:
      "Rewrite is working"

      If not you should see:
      "Rewrite is not working"

  4. In the example above only the RewriteEngine and the RewriteRule directive is used. However the rewrite engine can use more directives:

    RewriteEngine    {on|off}
    RewriteCond    TestString    CondPattern    [CondPattern Flag(s)]
    RewriteRule    RegExPattern     /url/to/redirect    [Rule Flag(s)]
    RewriteLog    LogFile
    RewriteLogLevel    LogLevel
    RewriteBase    URL-path
    RewriteOptions    Options
    RewriteLock    file-path
    RewriteMap    MapName     MapType:MapSource



    Directive Description server config virtual host per-directory htaccess
    RewriteEngine Enables or disables runtime rewriting engine.

    Default: RewriteEngine off

    Example 1: Usage.

    X X X X
    RewriteCond The RewriteCond directive defines a rule condition. Precede a RewriteRule directive with one or more RewriteCond directives. The RewriteRule directive will only take place if any one of the preceeding RewriteConds evaluate as true.

    TestString

    Is usually an URL or part of it or can contains expanded constructs.

    CondPattern

    CondPattern is the condition pattern i.e., a regular expression which is applied to the current instance of the TestString. The TestString is evaluated and then matched against CondPattern.

    CondPattern Flags

    Special flags can be set by appending a third argument between "[ ]" to the RewriteCond directive. If you wish to use multiple flags, you may delimit them by commas. The following flags can be used:

    • [NC]

      No Case - Make the condition pattern case insensitive.

    • [OR]

      OR condition - Allows a rule to apply if one of a series of conditions are true.

    X X X X
    RewriteRule The RewriteRule consists of a rewriting pattern and a url to redirect to.

    Rule Flags

    The rule flags can control the behaviour after a rewriting rule has been matched. The following rule flags can be used:

    • [R] (force Redirect)
      Redirect the to an external URL and send a 302 HTTP response (MOVED TEMPORARILY).

      Note:
      You can also do [R=301] to change the type. In this case a 301 HTTP response is send. The following types can be given:

      301 = Moved permanently
      302 = Moved temporarily
      403 = Forbidden
      404 = Not found
      410 = Gone

      Example 1: Usage [R].

    • [F] (force URL to be Forbidden)
      Force the current URL to be forbidden and send a 403 HTTP response (FORBIDDEN).

      Example 1: Usage [F].

    • [G] (force URL to be Gone)
      Force the current URL to be gone and send a 410 HTTP response (GONE).

      Example 1: Usage [G].

    • [L] (last rule)
      Force the rewriting proces to stop.

    • [P] (force proxy)
      Force the current URL as a proxy request and put through the proxy module mod_proxy.

    • [N] (next round)
      Rerun the rules again from the start.

    • [C] (chains)
      Chains a rewrite rule together with the next rule.

    • [T=mime-type] (T MIME-type)
      Use T MIME-type to force the file to be a mime type.

    • [NS] (no sub request)
      Use if no sub request is requested.

    • [QSA] query string append
      Use to add to an existing query string.

    • [NE] (no escapes)
      Turns off normal escapes that are default in the rewriterule.

      mod_rewrite escapes the # character into %23 which breaks the redirection.

    • [PT] (pass through)
      Pass through to the handler (together with mod_alias).

    • [S=3] (skip)
      Skip the next 3 rules.

    • [E=var:value]
      Set environmental variable var to value.

    X X X X
    RewriteLog Logs any rewriting actions it performs. If you do not want to use a log file, set the RewriteLogLevel to 0.

    Default: -None-

    LogFile

    The logfile name where the rewrite actions are stored.

    Example 1: Usage.
    Example 2: LogFile output.

    X X    
    RewriteLogLevel Set the verbosity level of the rewriting logfile. The higher the log level, the more your server will be slowed by the logging process.

    LogLevel

    Level 0 = no logging
    Level 9 = all actions are logged.

    Default: RewriteLogLevel 0

    Example 1: Usage.

    X X    
    RewriteBase Uses the directory as the starting point for all URL's. The directory must always start from the root directory.

    RewriteBase /xyz/abc   (correct)
    RewiteBase xyz/abc   (not correct)

    Example 1: Usage.

        X X
    RewriteOptions Sets either the inherit or the MaxRedirects options for the current per-server or per-directory configuration.

    X X X X
    RewriteLock Sets the file name for a synchronization lockfile, which mod_rewrite needs to communicate with RewriteMap programs.

    X      
    RewriteMap Defines a map, which can be used inside rule substitution strings by the mapping functions to insert/substitute fields through a key lookup.

    X X    


    Regular expression syntax Description
    . Any single character

    [abc] One of the characters (a or b or c)

    [^abc] None of the characters (not a or b or c)

    (text1|text2) text1 or text2

    a? Zero or one of a

    a* Zero or more of a

    a+ One or more of a

    a{4} Four times of a.

    a{4,} Four or more times of a.

    a{4,8} Between four and eight times of a.

    (...) Group section

    ^ Start matching at this point

    $ End point of the match

    !(pattern) Not prefix.

    \ char escape that particular char

    [a-d] Range of characters a,b,c,d



    Expanded constructs Description
    $N RewriteRule backreferences

    (0 <= N <= 9) which provide access to the grouped parts (parenthesis!) of the pattern from the corresponding RewriteRule directive (the one following the current bunch of RewriteCond directives).

    %N RewriteCond backreferences

    (1 <= N <= 9) which provide access to the grouped parts (parentheses!) of the pattern from the last matched RewriteCond directive in the current bunch of conditions.

    %{HTTP_USER_AGENT} HTTP header: HTTP_USER_AGENT

    Windows XP home edition, Firefox 1.07:
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7

    Windows XP home edition, Internet Explorer 6.0.29:
    Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Feedreader; .NET CLR 1.1.4322)
    %{ HTTP_REFERER} HTTP header: HTTP_REFERER

    %{HTTP_COOKIE} HTTP header: HTTP_COOKIE

    %{HTTP_FORWARDED} HTTP header: HTTP_FORWARDED

    %{HTTP_HOST} HTTP header: HTTP_HOST

    If URL = https://www.mobilefish.com/home/index.html
    HTTP_HOST is: www.mobilefish.com
    %{HTTP_PROXY_CONNECTION} HTTP header: HTTP_PROXY_CONNECTION

    For example: text/xml, application/xml, application/xhtml+xml, text/html; q=0.9, text/plain; q=0.8, image/png, */*; q=0.5
    %{HTTP_ACCEPT} HTTP header: HTTP_ACCEPT

    %{REMOTE_ADDR} Connection & Request: REMOTE_ADDR

    %{REMOTE_HOST} Connection & Request: REMOTE_HOST

    %{REMOTE_USER} Connection & Request: REMOTE_USER

    %{REMOTE_IDENT} Connection & Request: REMOTE_IDENT

    %{REQUEST_METHOD} Connection & Request: REQUEST_METHOD

    %{SCRIPT_FILENAME} Connection & Request: SCRIPT_FILENAME

    %{PATH_INFO} Connection & Request: PATH_INFO

    %{QUERY_STRING} Connection & Request: QUERY_STRING

    %{AUTH_TYPE} Connection & Request: AUTH_TYPE

    %{DOCUMENT_ROOT} Server Internals: DOCUMENT_ROOT

    %{SERVER_ADMIN} Server Internals: SERVER_ADMIN

    %{SERVER_NAME} Server Internals: SERVER_NAME

    %{SERVER_ADDR} Server Internals: SERVER_ADDR

    %{SERVER_PORT} Server Internals: SERVER_PORT

    %{SERVER_PROTOCOL} Server Internals: SERVER_PROTOCOL

    %{SERVER_SOFTWARE} Server Internals: SERVER_SOFTWARE

    %{TIME_YEAR} System Stuff: TIME_YEAR

    %{TIME_MON} System Stuff: TIME_MON

    %{TIME_DAY} System Stuff: TIME_DAY

    %{TIME_HOUR} System Stuff: TIME_HOUR

    %{TIME_MIN} System Stuff: TIME_MIN

    %{TIME_SEC} System Stuff: TIME_SEC

    %{TIME_WDAY} System Stuff: TIME_WDAY

    %{TIME} System Stuff: TIME

    %{API_VERSION} Specials: API_VERSION

    %{THE_REQUEST} Specials: THE_REQUEST

    %{REQUEST_URI} Specials: REQUEST_URI

    %{IS_SUBREQ} Specials: IS_SUBREQ

    %{HTTPS} Specials: HTTPS


  5. Restart Apache and try to access an existing page.