albrechtnate
albrechtnate

Reputation: 301

Rewrite urls to not include file extensions but allow subdomains to act normally

I need to

This is my current, hacked together htaccess file:

RewriteEngine On
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]

Sorry, this might be a duplicate or semi-duplicate. I looked around as much as I could but couldn't find anything that applied to my situation exactly or that I understood. Thanks guys!

Upvotes: 1

Views: 155

Answers (1)

anubhava
anubhava

Reputation: 784918

Try this code:

RewriteEngine On
RewriteBase /

# skip dev. subdomain
RewriteCond %{HTTP_HOST} ^dev\. [NC]
RewriteRule ^ - [L]

# calendar rule
RewriteRule ^calendar/?$ files/2013-2014_calendar.pdf [L]

# hide .html rules
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1/ [R=301,L,NE]

# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Upvotes: 1

Related Questions