1

I was getting an 'unknown error' when trying to access the admin. I couldn't see anything in the Craft logs apart from 404 related errors. I replaced the app folder but still no luck, eventually I discovered it was a rewrite rule in my .htaccess causing the problem.

# redirect PHP or HTM to no extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.(php|htm?)
RewriteRule ^ /%1 [L,R=301]

I'd love to understand why, and how I can get around this in the future?

Luke Pearce
  • 3,863
  • 1
  • 11
  • 25
sarah3585
  • 899
  • 7
  • 14
  • 1
    Out of interest - why are needing to do this? Craft can handle this for you. – Luke Pearce Aug 19 '16 at 14:00
  • I didn't know it could! Can you point me to where to look? – sarah3585 Aug 19 '16 at 14:05
  • It won't handle it per say but the convention when building a Craft site is to generally not use php files (http://craftcms.stackexchange.com/questions/722/can-we-use-php-in-our-templates-instead-of-twig). For htm you can just rename these to html and the extension will be removed automatically if placed in the template directory. – Luke Pearce Aug 19 '16 at 14:13

1 Answers1

2

It might be down to the order you have the code in your htaccess file? Did you try putting your code above the Craft rewrite code?

# redirect PHP or HTM to no extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.(php|htm?)
RewriteRule ^ /%1 [L,R=301]

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php?p=$1 [QSA,L]

Also I tend to exclude certain directories when I don't want rewrites to apply. One way to do this would be;

# ------------------------------------------------------------------------------
# | Exclude directories from the rewrite rules                                 |
# ------------------------------------------------------------------------------
RewriteRule ^craft - [L]
Terry Upton
  • 1,894
  • 10
  • 28