Data Koncepts

DK's Apache mod_rewrite Generator

Data Koncepts

DK's Apache mod_rewrite Generator

  • Home Page open submenu
    Data Koncepts'
      Home Page
  • Webmaster open submenu
    Professional
        Webmaster

    Development
        Process

    Small Website
        Fixed Price
        Offer

    Website
        Clients


    FREEBIES:
    Webmaster Security
        (see Security)

    Search For a
        New Host
        Checklist

    Search Engine
        Optimization
        w/mod_rewrite

    mod_rewrite
        Code Generator

    E-Mail
        Encrypter
  • Web Hosting open submenu
    Web Hosting
        Info
    & Checklist
    Web Hosting
        Offer
  • Security Updated! open submenu
    Online
        Security

    SuperScan v2New!
        Attack
        Detection
        & Reporting
    Hack Recovery
  • Professional Services open submenu
    Professional
        Documents

    Digital
        Imaging

    Screensavers
  • Computers open submenu
      Hardware
      Software
  • Contact open submenu
      Contact
      Terms &
        Conditions

      Sitemap
Website monitor by killerwebstats.com

Freedom Lost! Freedom!

DK's Apache mod_rewrite Generator

Last updated: March 11, 2019.

Apache's low cost and powerful set of features make it the server of choice around the world. One of its real treasures is the mod_rewrite module who's purpose is to redirect a visitor's request in the manner specified by a set of rules.

A companion to the Tutorial Article, this page will generate mod_rewrite code according to your specifications. That said, I abhore generators as they never seem to be sufficiently clairvoyant to guess the users' intentions. Therefore, this generator's code is provided as a basis for mod_rewrite coders to start their coding. It can NOT be ordered to avoid conflicting directives because of the diverse requirements of users. With that warning, you must use the code generated at your own risk. I strongly recommend that you understand all code before using it on a live website.

Remember, please, that the more that you have in your .htaccess file, the slower your server will run. Test your code using .htaccess then move code to the httpd.conf or httpd-vhosts.conf if at all possible (ask you host for help).

Security

Prevent Cross-site TRACE:
TRACE is a debugging method available on both Apache and IIS. Unfortunately, this little known function allows hackers to run scripts and capture cookies from your server (disable with "TraceEnable off" in your Apache's server configuration file). If you don't have access to your server configuration file, you can still protect your domain.

Block OR Ignore
(Block is highly recommended)

Limit Proxy Visitors:
i.e., prevent most proxy servers from hiding the origin (IP Address) of your visitors (a favorite trick of hackers).

Block OR Ignore
(Block is recommended)

Limit Bots:
i.e., prevent most web crawlers using your bandwidth to capture your content. Stop the ones without any identifier OR 'bot', 'crawl' OR 'spider' in their {USER_NAME}.

Block OR Ignore
(Block is recommended; do NOT extend the list excessively or it will slow the server to a crawl!)

Prevent Hotlinking:
Prevent stolen bandwidth by others displaying your images (or other scripts) from your website on their website.

Images All OR Ignore
(Block all hotlinks is recommended)

Enforce/Remove www. from host:
Secure server certificates are given to either the www. version of a domain name or the non-www. version. This code will change http://example.com => http://www.example.com OR
http://www.example.com => http://example.com

Force www. Remove www. OR Ignore this code

Enforce Secure Server:
Force use of your secure server for requests (after ensuring that your www. status matches your secure server certificate).

Secure Selective Secure OR Ignore
(The list of selective secure-protected PHP files will be shown as (file1|file2)\.php and must be replaced with your list of PHP scripts in a similar manner. This way, supporting files, i.e., css, js, images should retain the protocol of the script they're embedded in to prevent browser warnings.)

Miscellaneous

Custom 404:
Use a smart 404 file rather than the obvious "can not find" message. IF the file is not found, the request will be captured and provided to your handler file in a query string.


(include .php or .html)

Query String Handling:
Remove or retain a query string upon a redirection (by default, the query string is retained UNLESS a new query string is created).

Remove (kill) Retain (add) OR Ignore
(If the following key or key/value requirements are set, this will be ignored.)

Match query string key:
You may want to redirect if a query string key is present.



Match query string key/value pair:
Using the above example (handler.php?task=login), this code matches task=login on any file and redirects to another (handler) file.




(include .php or .html; query string will be preserved)

Time-of-Day Content:
i.e., serve differrent content depending up the TOD at the server.

(format for 6AM: 0600)
(format for 6PM: 1800)



(include .php or .html; time-of-day function relies upon the SERVER's time)

Navigation

Replace a character or string in the URI:
e.g., '-' => '_', 'a' => 'aaa' OR '.html' => '.php'


(character or text to be replaced - without quotes!)

(replacement string - without quotes!)

Add Extension to Filename:
To use an extensionless filename, this code will capture an extensionless filename (lowercase letters only) and add a file extension, i.e., http://example.com/goflyakite => http://example.com/goflyakite.php

.php .html OR Ignore

Filename to query string:
CMS's use a "handler file" which, when given a "filename" which it converts to a query string, will perform the specified task.
This code captures a filename (lowercase letters only) and sends it to a file handler as a query string), i.e.,
http://example.com/login => http://example.com/handler.php?task=login
and expand the matched letters to caps by adding A-Z, digits by adding 0-9, etc.,
resulting in [a-z]+ => [a-zA-Z0-9_]+


(handler file, e.g. handler.php above)

(key file, e.g., task above)

Redirect to new format:
When a novice webmaster discovers mod_rewrite, (s)he will use extensionless (or handler file) format URIs. Their problem becomes the loss of PR for their old format links. They then attempt to redirect the old format links to the new format links to the old format causing a loop. This code redirects from the old format to the new format (to update SE's) then to the script which can be served, i.e.,
handler.php?task=login => login => handler.php?task=login


(handler file, e.g., handler.php above)

(key name, e.g., task above)

Poor Man's RewriteMap:
When a massive change is made to a website, novice webmasters will typically create a long series of Redirect statements (or, if they planned the change, a small set of RewriteRules using regex) to redirect from the old links to the new. When the list of redirections, Redirect or RewriteRule is too long, a RewriteMap should be used to prevent a massive impact on the server. Because a RewriteMap can bring a server down, they can only be installed from the server or virtual host configuration files. Don't dispair! If a redirection is required, use a file handler to determine a redirection from a database file.


(include .php or .pl)

(handler file's query string key)
(Your handler file will be required to search a list for a pairing to match the original request. This can be as simple as an array within the file, a text file or accessing a database for a match. In any case, the redirection will have to be directed from the handler script using a header('Location:{url}', TRUE, 301); which, if a match is not found, redirects to your 404 script. Examples will be provided later.)


 

 
  This site designed, created, maintained and copyright © 1995 - 2025 by Data Koncepts.