0

I want to use one index.php file for all requests, except for files that exist eg. images, CSS and JS.

Also I want to force trailing slash on all requests.

Also I want to have one folder with system files that should not be accessible. But I don't want any one to see that this folder or these files exist.

Also I want all this to be as fast as possible.

I figured that I could use this htaccess:

RewriteEngine On

# Force trailing slash on the end
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]

# Handle all request from one index.php file
ErrorDocument 404 /index.php

# Prevent system folder from being accessed
RedirectMatch 404 ^/system/?$

In index.php I will decide if page exist or not and set response code accordingly.

I'm no pro, but it seems to work. But I wonder if ErrorDocument 404 /index.php will slowing anything down, eg. affecting how the server cache the request in the RAM memory?

Gordova
  • 347
  • 1
  • 9
  • 18
  • 1
    If you are trying handle all your request with index.php, is there even a need to do the RedirectMatch on system/? – Ghostff Nov 13 '19 at 21:34
  • In index.php I use some includes eg include('system/get-content-based-on-url.php') and I dont want any one to be able to access system/get-content-based-on-url.php. – Gordova Nov 13 '19 at 21:36
  • 1
    Here is what i mean. Lets say you are using a htaccess like this https://stackoverflow.com/questions/18406156/redirect-all-to-index-php-using-htaccess, in your index.php, you can just do a condition to check if url:path is `system/` and then redirect them to your 404 (most MVC framework routing pattern). but to answer your question, no it wont slow it down, you actually have a performance advantage. – Ghostff Nov 13 '19 at 21:45
  • 1
    Also it comes in handy when you have one control point. alternatively move the system/ directory out of public. – Ghostff Nov 13 '19 at 21:46
  • Glad to hear that "ErrorDocument 404 /index.php" gives a performance advantage! I like micro optimization. I could indeed store system files outside private_html. Great tip! Can you explain why "ErrorDocument 404 /index.php" gives a performance advantage? – Gordova Nov 13 '19 at 21:50
  • `ErrorDocument 404 /index.php` is a bad idea from a code readability point of view. Just rewrite everything that doesn't exist to index.php and let index set the 404 code if your router decides it's not a valid resource. `ErrorDocument` was not meant to be used that way and overwriting http response codes is just bad practice. – Capsule Nov 13 '19 at 23:10
  • 1
    @Capsule I agree on that ErrorDocument 404 should not be “abused” for routing purposes, [`FallbackResource`](https://httpd.apache.org/docs/2.4/mod/mod_dir.html#fallbackresource) would be a more appropriate way to handle this (if on an Apache 2.4) – 04FS Nov 15 '19 at 09:03

0 Answers0