.htaccess mod_rewrite rewrite examples

FREE THOUGHT · FREE SOFTWARE · FREE WORLD

.htaccess mod_rewrite rewrite examples

Here are some useful mod_rewrite rewrite examples that you can use in your .htaccess file. Some of my favorites.

Begin mod_rewrite in .htaccess

RewriteEngine On
RewriteBase /

Loop Stopping Code

Sometimes your rewrites cause infinite loops, stop it with one of these rewrite code snippets.

RewriteCond %{REQUEST_URI} ^/(stats/|missing\.html|failed_auth\.html|error/).* [NC]
RewriteRule .* - [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

Cache-Friendly File Names

This is probably my favorite, and I use it on every site I work on. It allows me to update my javascript and css files in my visitors cache’s simply by naming them differently in the html, on the server they stay the same name. This rewrites all files for /zap/j/anything-anynumber.js to /zap/j/anything.js and /zap/c/anything-anynumber.css to /zap/c/anything.css

RewriteRule ^zap/(j|c)/([a-z]+)-([0-9]+)\.(js|css)$ /zap/$1/$2.$4 [L]

SEO friendly link for non-flash browsers

When you use flash on your site and you properly supply a link to download flash that shows up for non-flash aware browsers, it is nice to use a shortcut to keep your code clean and your external links to a minimum. This code allows me to link to site.com/getflash/ for non-flash aware browsers.

RewriteRule ^getflash/?$ http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash [NC,L,R=307]

Removing the Query_String

On many sites, the page will be displayed for both page.html and page.html?anything=anything, which hurts your SEO with duplicate content. An easy way to fix this issue is to redirect external requests containing a query string to the same uri without the query_string.

RewriteCond %{THE_REQUEST} ^GET\ /.*\;.*\ HTTP/
RewriteCond %{QUERY_STRING} !^$
RewriteRule .* http://www.askapache.com%{REQUEST_URI}? [R=301,L]

Requiring a subdomain like www

This mod_rewrite code for your .htaccess will 301 redirect and request for askapache.com/anything to www.askapache.com/anything

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

Sending requests to a php script

This .htaccess rewrite example invisibly rewrites requests for all Adobe pdf files to be handled by /cgi-bin/pdf-viewer.php

RewriteRule ^(.+)\.pdf$  /cgi-bin/pdf-viewer.php?file=$1.pdf [L,NC,QSA]

Setting the language variable based on Client

For sites using multiviews or with multiple language capabilities, it is nice to be able to send the correct language automatically based on the clients preferred language.

RewriteCond %{HTTP:Accept-Language} ^.*(de|es|fr|it|ja|ru|en).*$ [NC]
RewriteRule ^(.*)$ - [env=prefer-language:%1]

Only allowing certain request_methods

This rewrite example denies any request with a request_method other than the specified request_methods with a 403 Forbidden HTTP status code.

RewriteCond %{REQUEST_METHOD} !^(POST|GET|OPTIONS|PUT)$
RewriteRule .* - [F,L]

«
»

Skip to Comments

Add Your Opinion

Reader Comments

  1. Jay-ar Bauson ~

    I was thinking of the ‘Cache-Friendly File‘ Names part, how do you actually apply it? What should be my directory structure?

    Let’s say in a directory I have my ‘index.php‘, ‘css’ folder and a ‘style.css‘ in it, how should I apply the tweak?

    index.php

    <?php
    function dump($str){
      echo "";
      print_r($str);
      echo "";
    }
    dump($_GET);
    ?>

    on its .htaccess I will put

    RewriteEngine on
    RewriteRule ^css/(j|c)/([a-z]+)-([0-9]+)\.(js|css)$ /css/$1/$2.$4 [L]

    I should update this line right? But how? :D

  2. bork ~

    Hi,

    I have the same problem as Jimmy: Want to remove a query string but your code does not work…. Thx for help.

  3. Jimmy ~

    Removing the Query_String
    Can you please give me an example of how i can test this thing. when i add this and go to example.com?test=123 it doesn’t do any thing so i do not know what it’s support to do; what i would like is to do is eliminate ?query from search engines

    email fake by the way -.-


It's very simple - you read the protocol and write the code. -Bill Joy

HTML | DCMI | GRDDL | XOXO | XDMP | XFN | DOM | XML | XHTML 1.1 Strict | CSS 2.1 | W3C | TLDP | WAI | DISA | ICSI | GIAC | SANS RR | GHOST | DEFCON | NIST | DHS CYBER | NIST | Phrack

↑ TOPExcept where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. HTTPD based on NCSA HTTPd

Site Map | Contact Webmaster | Email AskApache | Glossary | License and Disclaimer | Terms of Service