Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I've had problems with getting ErrorDocument to work reliably with rewrite errors, so I tend to prefer to handle invalid pages correctly in my rewrite cascade. I've tried to cover a fully range of test vectors with this. Didn't find any gaps.</p> <p>Some general points:</p> <ul> <li>You need to use the <strong>DOCUMENT_ROOT</strong> environment variable in this. Unfortunately if you use a shared hosting service then this isn't set up correctly during rewrite execution, so hosting providers set up a shadow variable to do the same job. Mine uses <strong>DOCUMENT_ROOT_REAL</strong>, but I've also come across <strong>PHP_DOCUMENT_ROOT</strong>. Do a phpinfo to find out what to use for your service.</li> <li>There's a debug info rule that you can trim as long as you replace <strong>DOCROOT</strong> appropriately</li> <li>You can't always use <code>%{REQUEST_FILENAME}</code> where you'd expect to. This is because if the URI maps to <code>DOCROOT/somePathThatExists/name/theRest</code> then the <code>%{REQUEST_FILENAME}</code> is set to <code>DOCROOT/somePathThatExists/name</code> rather than the full pattern equivalent to the rule match string.</li> <li>This is "Per Directory" so no leading slashes and we need to realise that the rewrite engine will loop on the <code>.htaccess</code> file until a no-match stop occurs.</li> <li>This processes all valid combinations and at the very end redirects to the <code>404.php</code> which I assume sets the 404 Status as well as displaying the error page.</li> <li>It will currently decode someValidScript.php/otherRubbish in the SEO fashion, but extra logic can pick this one up as well.</li> </ul> <p>So here is the <code>.htaccess</code> fragment:</p> <pre><code>Options -Indexes -MultiViews AcceptPathInfo Off RewriteEngine On RewriteBase / ## Looping stop. Not needed in Apache 2.3 as this introduces the [END] flag RewriteCond %{ENV:REDIRECT_END} =1 RewriteRule ^ - [L,NS] ## 302 redirections ## RewriteRule ^ - [E=DOCROOT:%{ENV:DOCUMENT_ROOT_REAL},E=URI:%{REQUEST_URI},E=REQFN:%{REQUEST_FILENAME},E=FILENAME:%{SCRIPT_FILENAME}] # redirect from HTTP://www to non-www RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] # remove php file extension on GETs (no point in /[^?\s]+\.php as rule pattern requires this) RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_METHOD} =GET RewriteRule (.*)\.php$ $1/ [L,R=301] # add trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*[^/]$ $0/ [L,R=301] # terminate if file exists. Note this match may be after internal redirect. RewriteCond %{REQUEST_FILENAME} -f RewriteRule ^ - [L,E=END:1] # terminate if directory index.php exists. Note this match may be after internal redirect. RewriteCond %{REQUEST_FILENAME} -d RewriteCond %{ENV:DOCROOT}/$1/index.php -f RewriteRule ^(.*)(/?)$ $1/index.php [L,NS,E=END:1] # resolve urls to matching php files RewriteCond %{ENV:DOCROOT}/$1.php -f RewriteRule ^(.*?)/?$ $1.php [L,NS,E=END:1] # Anything else redirect to the 404 script. This one does have the leading / RewriteRule ^ /404.php [L,NS,E=END:1] </code></pre> <p>Enjoy :-)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload