RewriteEngine On
Options +FollowSymlinks
RewriteBase /


# dude built a design browser. Cool.
# dude ran ads on it. Not as.
SetEnvIfNoCase Referer ".*coret.org$" BadReferrer
order deny,allow
deny from env=BadReferrer


# allow submissions.json to be executed as php
<FilesMatch "^.*submissions\.json$">
  SetHandler php-script
</FilesMatch>


# permanent redirect of old example files
RedirectMatch 301 ^/zengarden-sample\.css$ /examples/style.css
RedirectMatch 301 ^/zengarden-sample\.html$ /examples/index.html

# remove trailing .php from /examples/index.php
RedirectMatch 301 ^/examples/index/$ /examples/index.php

# clean up content page URLs
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

# REWRITE for CLEANER URL STRUCTURE
#
# The below rules accommodate /003/page5/ instead of ?cssfile=/003/003.css&page=5
# Other URLs in the structure:
# /003/
# /tr/zh-tw/
# /tr/zh-tw/003/
# /tr/zh-tw/page5/
# /tr/zh-tw/003/page5/
# /?css=http://external.css
# /legacy/?css=http://external.css
# /legacy/page5/?css=http://external.css
#
# Helpful mod_rewrite tips @ http://www.webmasterworld.com/apache/4229371-1-30.htm

# Rewrite requests to older URLs, e.g. ?cssfile=/003/003.css&page=5
RewriteCond %{QUERY_STRING} ^cssfile\=\/([0-9]+)\/[0-9]+\.css(&(page)\=([0-9]+))?
RewriteRule ^(.*)$ %1/%3%4/? [R=301]

# Resolve missing trailing slash for URLs with no filetype
RewriteCond $1 !(\.[a-z0-9]{1,5}|/)$ [NC]
RewriteCond $1 (..*)$ [NC] # so's we don't add a slash onto nothing and end up w/ '//'
RewriteRule ^(.*)$ $1/ [R=301]

# Remove excess trailing slashes
RewriteRule ^(.*)\/\/$ $1/ [R=301]

# Send traffic to index.php w/ appropriate variables
# we have to rename these to avoid creating an infinite loop
RewriteCond %{QUERY_STRING} !^css
RewriteRule ^(tr\/([a-z\-]+)\/)?(([0-9]+)\/)?(page(.*)\/)?$ index.php?css=$4&pg=$6&lang=$2 [L]

## External Stylesheets
# e.g. ?cssfile=http://www.skybased.com/css-zen-garden/css/screen.css
RewriteCond %{QUERY_STRING} ^cssfile\=(http[^&]+)
RewriteRule ^(.*)$ $1?css=%1 [R=301]

## External Stylesheets - send traffic to index.php w/ appropriate variables
# Note: pages using the legacy code are not translated
RewriteCond %{QUERY_STRING} ^css\=(http[^&]+)
RewriteRule ^(legacy\/)?(tr\/([a-z\-]+)\/)?(page([0-9]+)\/)?(.*)?$ $1index.php?css=%1&pg=$5&lang=$3 [L]
