I have setup a few "domain aliases" for an internet site that I am using as cookie-less sub-domain names, so static.domain.com/style.css serves exactly the same file as world wide web.domain.com/style.css.

However, if a person attempts to access static.domain.com/index.htm they must be 301 rerouted to world wide web.domain.com/index.htm. The mod_rewrite rules I've within the root web directory I figured works however they don't appear to become.

<IfModule mod_rewrite.c>

    RewriteEngine On

    # "/res/all.20110101.css" => "/res/all.css"

    RewriteCond % !-f

    RewriteCond % !-d

    RewriteRule ^(.+).(d+).(jscsspngdigitaljpegpresen)$ $1.$3 [L]

    # Aside from "static" sub-domain names, pressure "world wide web" when utilized without

    RewriteCond % .

    RewriteCond % !^www.domain.com [NC]

    RewriteCond % !^s-img.domain.com [NC]

    RewriteCond % !^static.domain.com [NC]

    RewriteRule (.*) http://world wide web.domain.com/$1 [R=301,L]

    # If file asked for is HTML, pressure "world wide web"

    <FilesMatch ".(htmhtmlphp)$">

        RewriteCond % .

        RewriteCond % !^www.domain.com [NC]

        RewriteRule (.*) http://world wide web.domain.com/$1 [R=301,L]

    </FilesMatch>

</IfModule>

This can redirect every request which doesn't choose static files:

RewriteCond % !^www.domain.com$ [NC]

RewriteCond % !.(jscsspngdigitaljpegpresen)$ [NC]

RewriteRule (.*) http://world wide web.domain.com/$1 [R=301,L]

It reads:

  • IF hostname isn't world wide web.domain.com
  • AND asked for file doesn't finish by having an permitted extension
  • Then redirect towards the master (world wide web) domain

Also for the versioning (you'll need the non-greedy (.+?) because .+ would eat all of your string there could be no match for that pattern):

RewriteCond % !-f

RewriteCond % !-d

RewriteRule ^(.+?).([-9]+).([a-z]+)$ $1.$3 [L]

After playing around a little with vbence's answer, I happened upon a far more functional solution, although I am unsure whether it's probably the most optimized one.

<IfModule mod_rewrite.c>

   RewriteEngine On

   # Demands for "/res/all.20110101.css" offer "/res/all.css", etc.

   RewriteCond % !-f

   RewriteCond % !-d

   RewriteRule ^(.+?).([-9]+).([a-z]+)$ $1.$3 [L]

   # When the hostname is not world wide web.domain.com AND asked for file's extension

   # is not within the filter list, redirect towards the world wide web.domain.com version.

   RewriteCond % !^www.domain.com$ [NC]

   RewriteCond % !.(jscsspngjpe?gpresen)$ [NC]

   RewriteRule (.*) http://world wide web.domain.com/$1 [R=301,L]

</IfModule>