Computers And Industry

Apache .htaccess Example For Friendly URLs

To handle friendly URLs of the type:

One must employ the use of an .htaccess file located in the root document folder (or some other folder that contains the processing scripts).

What the .htacess file does is perform a change from the friendly url as listed in the browser address bar to some other url that the scripting understands. For example, one might have a friendly url of the form:

that one wishes to be transformed into a url of the form


And furthermore, one desires that the original url remain in the browser address bar instead of the newly transformed url (i.e. no redirect).

Therefore, we need to use the apache mod_rewrite module. This module, in conjunction with the instructions in the .htaccess file, rewrites the URL before it hands it off to the scripting engine or resource handler for processing.

The following .htaccess file does what is required above.

ErrorDocument 401 default

  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php\?term=$1 [L] 

File Description

Line by line description is as follows (note we are not going to use a table for presentation because of mobile).

Error Document 401 default

This handles the user id and password prompt that pops up with an .htaccess protected subfolder.

This checks if mod_rewrite is installed on the server. If it is, great, it will execute the instructions contained between the opening and closing tags. Otherwise, it will skip it all. NOTE. If you don't do this, and the server doesn't have mod_rewrite installed, you'll get a server 500 error.

RewriteEngine on

This starts up the rewrite engine.

RewriteBase /

Sets the base directory of the processing engine to the server root.

RewriteCond %{REQUEST_FILENAME} !-f

This asks if what is in the address bar is not a file name. If it is not, then set the rewrite condition to true.

RewriteCond %{REQUEST_FILENAME} !-d

This asks if what is in the address bar is not a folder. If it is not, then set the rewrite condition to true.

RewriteRule ^(.*)$ index.php\?term=$1 [L]

This does the actual rewrite. What it does is take everything after the host name, signified by the code (.*) a.k.a. sillyputty and puts it into the $1 parameter of the transformed url. For example, it will take the url

and put sillyputty into the search term position like so:


And then mod_rewrite stops processing (signified by the [L] parameter).

The closing processing instruction tag.

End Of Processing

Once the file is done processing, it now hands off the newly transformed url to the scripting engine where one can process that get query parameter term accordingly.

NOTE!!!!! Put a in your output html just in case you have multiple subfolders as part of your friendly url. The browser will think the current folder is the one listed as part of the friendly url and therefore all the relative image references, relative stylesheet references, etc. will get messed up.

NOTE!!!!! Think of the sequential RewriteCond statements as one big iterating AND loop that starts out with a true value. i.e...

(Step 1). True AND'ed with result of rewritecond 1 equals Result (Step 1).

(Step 2). Result (Step 1) AND'ed with result of RewriteCond2 statement = Result (Step 2).

(Step 3). Result (Step 2) AND'ed with result of RewriteCond3 statement = Result (Step 3).

And so on and so on....with the last result being either true to invoke the RewriteRule or false to not invoke the rewrite rule.

And Finally....

The .htacess file is a crazy complex thing. If you want to know more, read up on the apache mod_rewrite at the apache site and you will also need to read up on RewriteCond, RewriteBase, and RewriteRule directives as those are responsible for making the matches that trigger the rewrite, url substitutions, etc.
Open Source

Paul F. Sirpenski
Personal Open Source Directory Of Paul F. Sirpenski

Open Source directory Of the Microsoft Asp.Net Core project.

Developed By Paul F. Sirpenski. Copyright 2021.