Redirect missing pages. Instead of showing frustrating messages like “401 Page Not Found” you can show your users a neat html pages showing a humble message like “Your request seems to be having wrong structure. Please check your URL“. Or “We are under maintenance. Please be back after some time“.

Lets start writing our .htaccess file. We should put two lines (Options +FollowSymlinks and RewriteEngine on) in almost all .htaccess files to start the writing engine (details out of scope). I will skip these two lines in my examples, please remember to have them in your file. The third line specifies which directory to start from (make base directory). Note: In case you are putting htaccess file in some sub directory then RewriteBase should be written to point to that sub-directory else rewritten URLs (targets) will be relative to home directory of the domain.

1) How to rewrite http://www.host.com/somefile.html to http://www.host.com/somefile.php

Therefore our rewrite rules would match only filenames & paths which are just after http://www.host.com/ (here somefile.html)

^, (.*), $ and $1 are all regular expression special characters having special meaning e.g, ^ is to match the start of the string (it is start anchor), similarly $ is end anchor. Do not get confused with $1, it is not an anchor. It carries the back-referenced data captured with the parentheses “(” and “)”. A dot “.” matches any character and an asterisk following this dot means zero or more characters. A dot preceded by a back slash \. matches the literal dot (and not any character). For more info please refer regular expression tutorials.

2) How to rewrite all language files to a language directory, say, http://www.host.com/file.html to http://www.host.com/french/file.html

RewriteRule ^(.*)\.html$ french/$1.html

3) How to permanently redirect old URLs to new ones (to secure old page ranks)

This way we can either strip www. from our domain (or add it, if you want) to put away ambiguity (between http://domain.com and http://www.domain.com) for crawlers and maintain a uniform linkage with external websites. For crawlers both are different entities.

This can be used to redirect (not only rewrite) a user to readable URL. [R] handles redirection. %{QUERY_STRING} a global variable, contains the whole query string. This can be used to support some old URL patterns (backward compatibility). Though the reverse of it would be used to show and store the human readable URLs while at server you still process them with old functionality, eg. your users remember and write URL like file/category/carnivores while you still process them with file.php?id=123

In this case if anybody requests a page which is not there on the server, s/he would be presented my-401-page.php, which can do some logging task also.

6) Limit the number of simultaneous connections to a directory or whole website (homepage) by,

MaxClients 100

7) In case of error, you can show your users an email of system administrator

ServerAdmin admin@mydomain.com
How to debug rewrite rules in htaccess file?

a) Mostly, the problem could be that AllowOverride is not set to allow configuration directives.

You should make sure an AllowOverride None is not in effect for the file scope in question by putting garbage in your .htaccess file and reload. If a server error is not generated, then you almost certainly have AllowOverride None in effect.

b) Check the apache error log file (check through cpanel) at the sever.

c) Use the [R] option (which redirects the browser) to redirect the rewritten URL into the browser’s address bar. This URL can be appended with the matched back-references. Hence we get the regular expression matched strings in URLs e.g.,

RewriteEngine On
RewriteRule ^(.*)$ /file.php/category/match=$1 [R]

It will paste /file.php/category/match=”matched value here” in the address bar.

How to block Use htaccess file. this is run by server and ensured by the server that the rules written in it are enforced. On the other hand, robots.txt file is just a guideline file. Robots are not bound to follow it. To ensure denial of all requests for the restricted directory, put the following […]

How to stop indexing of files in a directory using .htaccess? Sometimes we do not want to show all the files in a directory (which by default is a behavior of web servers). That can be done using htaccess file. Though there’s a simple solution to this, put an index.html file showing a decent message […]

IE (Internet Explorer) doesn’t work with user names and passwords in Web site addresses URLs (HTTP/S). Though, IE versions 3.0 to 6.0 support this. The following URL syntax may not work in Internet Explorer or in Windows Explorer, http(s)://username:password@mydomain.com Reason: Such URL syntax is used to automatically send username/password to a Web site which requires […]

Sometimes sever settings do not allow you to upload files more than a fixed limit (usually 2 MB). But, some websites might require allowing files more than this limit. In such cases we need to set some options in httpd.conf configuaretion file. This sometimes is not allowed on shared servers. “.htaccess” file comes to the […]

Sometimes we need to send e-mails from UNIX command-line itself. Sometimes we need to write a small script to do so. Here’s what we need to do for such tasks. We can send mail in UNIX using “mail” command. Syntax is as follows, mail -s “Subject” -c “any_cc_email_id” -b “any_bc_email_id” “comma_separated_To_email_id” But the above syntax […]

How to check if a value exists in array or not in PHP? PHP function in_array() checks if a value exists in an array in a case-sensitive manner and returns boolean result. e.g., <?php $os = array(“Mac”, “NT”, “Unix”, “Linux”); if (in_array(“Unix”, $os)) { echo “Got Unix”; } ?> How to check if a key/index […]

A random string with length specified as a parameter ($sLength) and having allowable characters in another parameter ($a). <?php $s = “”; //random string $sLength = 35; //random string length (you can put any number, you like your string to be in length) //allowable chars in the wanted random string (add other chars but avoid […]

I have listed a few methods in PHP to check the existence of a file on the web. <?php $fileURL = “http://www.some-domain.com/image.jpg”; echo “Checking the existence of $fileURL<br>”; ?> Method 1: <?php $a = microtime(true); //start time in microseconds of 1st method echo date(“d/m/y : H:i:s”, time()), ” “,$a, “<br>”; get_http_response_code($fileURL); //fast but not fool […]

To remove/trim the last/trailing or first/heading comma from a string, one should first confirm that the last character of the string is comma itself and nothing else. This would make the code foolproof and prevent accidental removal of characters other than comma. A few methods are described here and listed in the order of increasing […]