Regular expressions 101

Filed under: htaccess, PHP, Misc. Tips

What are regular expressions?

A regular expression is a special string used to describe or match a search pattern or set of strings. Regular expressions use certain syntax rules as outlined in the section below, “Basic regular expressions syntax”.

Regular expression can be abbreviated as:

  • regexp
  • regex
  • regxp

Plural forms of regular expressions are:

  • regexps
  • regexes
  • regexen

(Read more…)

Password protection of web pages

Filed under: htaccess

There are several methods to password protect a web page, some are more secure than others. One method is by using cookies and PHP, an example of this is the php script developed by Zann Marketing, Simple Authorization Script (SAS). This small and simple script allows users to password protect web pages without using a database. This feature of SAS is great for webmasters who do not have access to databases such as MySQL.

Although SAS is quite “secure”, it is not the most secure form of password protection. The most secure form of password protection is through the use of .htaccess and .htpasswd files. (Read more…)

Parsing PHP on .html pages

Filed under: htaccess, PHP

There are 2 ways of getting your .html pages to parse PHP, this is via:

  1. .htaccess
  2. httpd.conf

In both methods above, you need to make sure that your server supports PHP.

Which method to choose?

For most webmasters who uses shared hosting and do not have access to their server’s configuration files, you would use method one, via .htaccess. If you have access to your server’s configuration files, that is, you have a dedicated server and can access httpd.conf, then use method two, via httpd.conf.

(Read more…)

Stop Image Hotlinking using .htaccess

Filed under: htaccess

Image hotlinking is when another web site links to your images (usually without your permission or knowledge). Most webmasters don’t appreciate others linking to their images as this uses up their bandwidth and can end up costing the webmaster quite some money.

Using htaccess a mod rewrites, you can:

  1. Ban all hotlinkers
  2. Ban one particular hotlinker
  3. Show a different image to one particular hotlinker

(Read more…)

Permanent redirects using htaccess

Filed under: htaccess

Firstly, before you start any rewrites, make sure your rewrite engine is turned on. The following line of code should be one of the first lines in your .htaccess file, before any RewriteCond, RewriteRule and redirect, etc.

RewriteEngine On

To permanently redirect a single page, simply add the line:

redirect 301 old-url new-url

  • “redirect” tells Apache what to do.
  • “301″ is your status code for permanent redirect (status codes are optional, but preferable).
  • “old-url” is the old page you want to redirect.
  • “new-url” is the new landing page when the old-url is requested.

To permanently redirect your root page, for example, you have chosen http://www.yourdomain.com/ as your root page and want all requests to http://yourdomain.com/ to permanently redirect to http://www.yourdomain.com/, use the following code:

RewriteCond %{HTTP_HOST} !^www\..* [NC]
RewriteCond %{HTTP_HOST} !^[0-9]+\.[0-9]+\..+ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [R=301,L]

  • First line: If the host doesn’t begin with www.
  • Second line: If the host is not an IP.
  • Third line: Then rewrite the host to have http://www. in front.

Use a custom error document

Filed under: htaccess

Web servers have a default error document that it serves in cases of requests for pages that don’t exist. One can create a custom error document and tell the web server to use the custom error document instead of its default error document.

Steps to take to use a custom error document

  1. Create a custom error document. To view Zann-Marketing.com’s custom error document, click here.
  2. Add this line of code in your .htaccess file (preferably near the beginning) and your web server will use the custom error document when it can’t find the page requested.

    ErrorDocument 404 /your_error_document.html

DirectoryIndex

Filed under: htaccess

The DirectoryIndex directive sets the URL to be returned when the index of a directory is requested by a visitor, that is, when a visitor specifies a / at the end of the directory name.

http://www.zann-marketing.com/

When there is a request for the index of a directory, the server usually returns the URL index.html. By using DirectoryIndex, one can specify a different URL to be returned when the index of a directory is requested, eg index_2.html.

Example:

DirectoryIndex index_2.html

In the above example, when the index of the directory is requested, the server will return the URL index_2.html, instead of index.html.

One can specify more than one URL for the DirectoryIndex and the server will return the first URL it finds. For example:

DirectoryIndex index.html home.html start.html

In the example above, the server will try to return index.html first, and if index.html isn’t found, then it will try to return home.html, and if that fails, it will try to return start.html.