# encoding: UTF-8
# api: apache
# title: RewriteRules
# description: Map paths onto dispatcher script
# version: 1.0
# depends: mod_rewrite


Options -MultiViews
RewriteEngine On

#-- Simulate [END] flag
RewriteCond  %{ENV:REDIRECT_STATUS}  =200
RewriteRule  ^                  -                       [L,NS]

#-- Strip www. prefix
RewriteCond  %{REQUEST_METHOD}  ^GET$
RewriteCond  %{HTTP_HOST}       ^ww+\.(\w+\.\w+)\.?$
RewriteRule  ^(.*)$             http://%1/$1            [R=301,QSA,L]
#-- Consolidate to just freshcode.club for now
RewriteCond  %{REQUEST_METHOD}  ^GET$
RewriteCond  %{HTTP_HOST}       ^(freshmeat|freecode)\.club$
RewriteRule  ^(.*)$             http://freshcode.club/$1   [R=301,QSA,L]

#-- RSS/Atom aliases
RewriteCond  %{QUERY_STRING}    ^format=(atom|rss|json)$
RewriteRule  ^$ feed/xfer.%1
RewriteRule  ^(?:projects)\.(atom|rss|json)$  feed/xfer.$1

#-- Freecode API mapping
RewriteCond  %{REQUEST_METHOD}  ^GET$
RewriteRule  ^projects/([\w-_.]+)\.json$  index.php?page=api&name=$1&api=query [L,NS,QSA]
RewriteCond  %{REQUEST_METHOD}  ^PUT|POST$
RewriteRule  ^projects/([\w-_.]+)\.json$  index.php?page=api&name=$1&api=update_core [L,NS,QSA]
RewriteCond  %{REQUEST_METHOD}  ^CREATE|INIT$
RewriteRule  ^projects/([\w-_.]+)\.json$  index.php?page=api&name=$1&api=new_project [L,NS,QSA]
RewriteCond  %{REQUEST_METHOD}  ^POST$
RewriteRule  ^projects/([\w-_.]+)/releases\.json$  index.php?page=api&name=$1&api=publish [L,NS,QSA]
RewriteCond  %{REQUEST_METHOD}  ^(GET|DELETE)$
RewriteRule  ^projects/([\w-_.]+)/releases/(\w+)\.json$  index.php?page=api&name=$1&api=version_%1&id=$2 [L,NS,QSA]
RewriteCond  %{REQUEST_METHOD}  ^(GET|PUT|POST|PUSH)$
RewriteRule  ^projects/([\w-_.]+)/urls\.json$  index.php?page=api&name=$1&api=urls [L,NS,QSA] 


#-- Temp disable
#RewriteRule  ^               error500.html                       [L]



#-- Page dispatching
RewriteRule  ^index/?$							/			[R=301,L]
RewriteRule  ^$								index.php?page=index    [L,NS,QSA]
RewriteRule  ^(projects|submit|search|flag|feed|admin)\b/?(\w+(?:[-_]\w+)*(?:\.\w{1,7})?)?(?:\.(json|atom|rss))?/?$   index.php?page=$1&name=$2&ext=$3   [L,NS,QSA]
RewriteRule  ^(search|flag|names?|tags?|login|links|rc)(?:/(\w+)?)?$	index.php?page=$1&name=$2	[L,NS,QSA]
RewriteRule  ^(github-releases|launchpad-releases|drchangelog)/?$	index.php?page=$1	[L,NS,QSA]
RewriteRule  ^(ssl)/?$							index.php?page=$1	[L,NS,QSA]
RewriteRule  ^(url|go)/(\w+(?:[-_.]\w+)*)/(\w+)/?$			index.php?page=redir&name=$2&ext=$3	[L,NS,QSA]
RewriteRule  ^(forum|meta)\b/?(\w+)?/?$					page_forum.php?name=$2	[L,NS,QSA]

#-- Deny direct invocations
RewriteRule  ^freshcode\.db.*$  -                       [F]
RewriteRule  ^\.                -                       [F]
RewriteCond  %{ENV:REDIRECT_STATUS}  !200
RewriteRule  ^\w+\.php(|/.*)$   -                       [F,L,NS]


#-- invalid resources
RewriteRule ^favicon\.(png|ico|jpeg)$ http://freshcode.club/img/changes.png [R=301,L]


