Skip to content

Instantly share code, notes, and snippets.

@fideloper
Created April 27, 2012 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fideloper/2509880 to your computer and use it in GitHub Desktop.
Save fideloper/2509880 to your computer and use it in GitHub Desktop.
htaccess to display markdown only if file exists
#Turn on Rewrite
RewriteEngine On
#Check if requested file ends in .md or .markdown
RewriteCond $1 \.(md|markdown)$ [NC]
#Check if file exists
RewriteCond %{REQUEST_FILENAME} -f
#Rewrite so $_GET['file'] is available to PHP code
RewriteRule ^(.*)$ /index.php?file=$1 [L]
<?php
/*
For files in web root:
index.php
file.md
file2.md
**********************************/
/*
Allows users such as: http://mysite.com/file.md
Which would effectively become: http://mysite.com/index.php?file=file.md
**********************************/
/*
Files which do not exist will give a 404 error
http://mysite.com/some_random_file.md #Yields 404 error
http://mysite.com/index.php?file.md #Works - Still need to check in code if file_exists();
**********************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment