≡ Menu

mod rewrite

Drupal is modular framework and content management system (CMS) and works under Lighttpd too. By default, Drupal passes path arguments to itself via its internally generated URLs. This results in URLs that look like the following: "http://www.example.com/?q=node/83." This can make URLs hard to read and it also stops many search engines, like Google, from indexing the pages with these URLs.

You can tell Drupal to use "clean URLs", eliminating the "?q=" in internal URLs. Assuming that your site hosted in rootdirectory itself, open your lighttpd.conf file or domain configuration file:
# vi lighttpd.conf
Make sure mod_rewrite is enabled:
server.modules += ( "mod_rewrite" )
Append following configuration directives:

url.rewrite-final = (
  "^/system/test/(.*)$" => "/index.php?q=system/test/$1",
  "^/system/test-clean-url/(.*)$" => "/index.php?q=system/test-clean-url/$1",
  "/rss.xml$" => "/index.php?q=rss.xml",
  "^/search/(.*)$" => "/index.php?q=search/$1",
  "^/([^.?]*)\?(.*)$" => "/index.php?q=$1&$2",
  "^/([^.?]*)$" => "/index.php?q=$1",
}
# Error 404
server.error-handler-404  = "/index.php"
 
# stop these
url.access-deny = ( "~", ".inc", ".engine", ".install", ".module", ".sh", "sql", ".theme", ".tpl.php", ".xtmpl", "Entries", "Repository", "Root" )

Save and close the file. Restart lighttpd, enter:
# /etc/init.d/lighttpd restart
Feel free to adjust rules as per your setup.

Further readings:

Many of our regular readers like to know more about lighttpd hotlink protection using mod_rewrite. Lighttpd can use HTTP referrer to detect hotlink and can be configured to partially protect hosted media from inline linking, usually by not serving the media or by serving a different file.

Lighttpd anti hotlinking configuration - redirect to another media

Open lighttpd.conf configuration file:
# vi /etc/lighttpd/lighttpd.conf
Append the following directive to redirect to a default picture called /hotlink.png:

$HTTP["referer"] =~ ".*BADDOMAIN\.com.*|.*IMAGESUCKERDOMAIN\.com.*|.*blogspot\.com.*" {
  url.rewrite = ("(?i)(/.*\.(jpe?g|png))$" => "/hotlink.png" )
}

So if anyone from *.blogspot.com linked www.cyberciti.biz/image.png it will be replaced with www.cyberciti.biz/hotlink.png. I've written small script to detect excessive hotlink from log file and ban all those domains. Most types of electronic media can be redirected this way, including video files, music files, and animations etc.

Related: Apache web server user can stop leechers using mod_rewrite / .htaccess rules.

Apache mod_rewrite examples for new Linux / UNIX admin

Apache’s mod_rewrite considered as one of the difficult module to configure and use. This article will lead you through rewrite rules, regular expressions, and rewrite conditions, and provide a great list of examples:

Apache's low-cost, powerful set of features make it the server of choice for organizations around the world. One of its most valuable treasures is the mod_rewrite module, the purpose of which is to rewrite a visitor's request URI in the manner specified by a set of rules.

=> Learn Apache mod_rewrite: 13 Real-world Examples

When you are updating website you may need to send error 503 to client. Error 503 indicates that web server is temporary out of service or down. This is useful if you are running popular database driven website such as a forum or e-commerce site. So when the site is under maintenance you can send user a good message indicating that site is down for some work.

This tip will help you to disable a site for maintenance using mod_rewrite without redirecting url.

Both Lighttpd and Apache webserver allows you to send this message to client using server side rewriting using mod_rewrite and php.

Error 503 means

=> Server is too busy to serve your request
=> Server is slashdotted or dugg to death
=> Server is forced to send this message etc

But why to send error 503?

Error 503 informs search engine that site is temporary out of service. This is quite important for site which is heavily depends upon search engine for selling products and services.

PHP code - down.php

The down.php file simply sends a 503 HTTP error header indicating that service is unavailable. echo command sends a message for your site.

So first create a php file as follows:
<?
header("HTTP/1.1 503 Service Unavailable");
echo "Server is down for maintenance. We will be back in 60 minutes.";
?>

Task: Lighttpd send error 503 when web is under maintenance

Copy down.php to webserver documentation root:
cp down.php /home/lighttpd
OR
cp down.php /var/www

Open lighttpd configuration file
# vi /etc/lighttpd/lighttpd.conf
Now add following code:

$HTTP["url"] != "/down.php$" {
   url.rewrite = ( "" => "/down.php" )
}

Save and close the file.

It is also possible to block site for rest of the world except for website developer or site admin IP address:

$HTTP["remoteip"] != "201.203.149.149" {
                   url.rewrite = ( "" => "/down.php" )
}

Restart lighttpd:
# /etc/init.d/lighttpd restart

Task: Apache send error 503 when web is under maintenance

You can use an Apache mod_rewrite module. Open .htaccess file
vi .htaccess
Append following code:
RewriteEngine on
RewriteRule !down.php$ /down.php [L]

Again it is possible to block site for rest of the world except for website developer or site admin IP address:
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^201\.203\.149\.149$
RewriteRule !down.php$ /down.php [L]

Save and close file. Restart Apache 2 web server:
# /etc/init.d/httpd restart
OR
# /etc/init.d/apache2 restart

Let us say user hits url http://domain.com/dir/somefile.html
First rules will check for client IP address - 201.203.149.149. If match found user is allowed to browse site. Otherwise user will get 503 error with message:

Server is down for maintenance. We will be back in 60 minutes.