Skip to main content

Apache 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();
# **********************************/