Loading...
Real Time Concepts

Apache Mod_Rewrite

Installation

mod_rewrite is a standard Apache module configured by default.

Configuration

To allow RewriteRules in .htaccess files, you need a minimum of AllowOverride FileInfo in the VirtualHost or Directory config.

Troubleshooting

1. Check that rewrites actually work by creating a simple rule that redirects from /rackspacetest to www.google.co.uk

RewriteRule ^rackspacetest http://www.google.co.uk/ [L,R]

2. Enable the RewriteLog and review the output with the Customer.

RewriteLog /home/path/to/desired/log/file.txt
RewriteLogLevel 5

Helping Customers create rules

If you are comfortable with RewriteRules, you should aim to offer advice not implementation, as technically they fall under the category of site content rther than configuration and are predominantly configured in .htaccess files.

  • Advice: Sam in #100519-00317 … e.g. Frans in #090918-01148 … e.g. Frans in 091005-01402
  • Comment out broken rules: Hans in #100519-00317

Give a man a RewriteRule and you solve his problem for one day, Teach a man to RewriteRule and you solve his problem for life.

Here is an excellent guide to give to Customers wanting to learn more: http://httpd.apache.org/docs/current/rewrite/

Some simple RewriteRule examples

  • How to rewrite all web request on my site without www to www.domain.com

RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule (.*) http://www.example.com$1 [R,L]

  • How to redirect all web requests on port 80 (or HTTP) to port 443 (HTTPS)

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{SERVER_NAME}/$1 [L,R]

  • How to disable TRACE and TRACK methods on Apache

RewriteCond %{REQUEST_METHOD} ^(TRACE|TRACK)
RewriteRule .* – [F]

  • How to exclude mod_status from being rewritten by existing rules (placed before the problem rule)

RewriteCond %{REQUEST_URI} !=/server-status

  • How do I redirect all web requests on www.example.com/pub to www.example.com/article/pub

RewriteCond %{http_host} ^[www\.]*example\.com
RewriteRule ^pub(/)?$ /article/pub [R=301,L]

  • Force all URLs to be lowercase

RewriteEngine On
RewriteMap lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L

Leave a Reply

Your email address will not be published. Required fields are marked *