Posted by CANbike on Sun, 22 Sep 2013

[.htaccess] Hide .php Extension, Set Directory Index, Eliminate Duplicate Content, etc.

Add www to Domain Name-thumb.jpgHide php extension-thumb.jpgSet-directory index-and-404 error message-thumb.jpgmod rewrite-quick reference-thumb.jpg

The following list some notes, tips, and tricks used to configure a .htaccess file for the directory Directory hash contains hash.php, 404.html, .htaccess and some other php files.

.htaccess Template File

AddDefaultCharset UTF-8
Options -Indexes +FollowSymLinks

<IfModule mod_rewrite.c>

	RewriteEngine On
	RewriteBase /hash/


Add “www.” to Domain Name

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$$1 [R=301,L]

Custom 404 Error Message

404.html contains a custom Content Not Found (404) error message. Use ErrorDocument.

ErrorDocument 404 /404.html

DirectoryIndex Duplicate Content

PHP extension is hidden. If DirectoryIndex hash.php is used, duplicate content is formed.

  • ->

Both and display the content of hash.php.

Eliminate duplicate content by ReWriting to

# Redirect hash.php requests to directory base URL
RewriteCond %{THE_REQUEST} ^(.+)/hash\ HTTP/
RewriteRule ^(.+)$ /hash/ [R=301,L]

Hide the .php Extension

Note the template setting of RewriteBase /hash/. Otherwise, adjust $1 to$1.

# If not a directory, remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]

# Redirect external .php requests to an extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=301,L]

# Resolve extensionless php url, to the php file
RewriteRule ^([^/.]+)$ $1.php [L]

mod_rewrite: Documentation

Apache Module mod_rewrite


Rewrite Flags

Regular Expression (regex): Documentation

mod_rewrite: Quick Reference

Rewrite Flags: Quick Reference

L Causes mod_rewrite to stop processing the rule set
NC Causes the RewriteRule to be matched in a case-insensitive manner
R Causes a HTTP redirect to be issued to the browser

Regular Expression (regex): Quick Reference

. Matches any single character
+ Repeats the previous match one or more times
* Repeats the previous match zero or more times
? Makes the match optional
^ Called an anchor, matches the beginning of the string
$ The other anchor, this matches the end of the string
( ) Groups several characters into a single unit, and captures a match for use in a backreference
[ ] A character class – matches one of the characters
[^ ] Negative character class – matches any character not specified

RewriteCond Server-Variables: Quick Reference

HTTP_HOST The domain name of the server (for virtual hosting), and the TCP port number on which the server is listening
REQUEST_FILENAME The full local filesystem path to the file or script matching the request
THE_REQUEST The full HTTP request line sent by the browser to the server (e.g., “GET /index.html HTTP/1.1”)

Set the Directory Index

Use DirectoryIndex.

DirectoryIndex hash.php

500 Internal Server Error

Hostgator’s server did not like php_flag magic_quotes_gpc off and gave a “500 Internal Server Error“ message. The cause was PHP being upgraded from 5.3.x to 5.4.x.

magic_quotes_gpc was deprecated as of PHP 5.3.0 and removed as of PHP 5.4.0. See Magic Quotes.

Problem was fixed by commenting out or deleting the line php_flag magic_quotes_gpc off, as it is no longer required.

Related Item(s):