Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I came up with this:</p> <pre><code>DirectorySlash Off RewriteEngine on Options +FollowSymlinks ErrorDocument 404 /404.php #if it's www # redirect to non-www. RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,QSA] #else if it has slash at the end, and it's not a directory # serve the appropriate php RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1.php [L,QSA] #else if it's an existing file, and it's not php or html # serve the content without rewrite RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{REQUEST_FILENAME} -f RewriteCond %{REQUEST_URI} !(\.php)|(\.html?)$ RewriteRule ^ - [L,QSA] #else # strip php/html extension, force slash RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^(.*?)((\.php)|(\.html?))?/?$ /$1/ [L,NC,R=301,QSA] </code></pre> <p>Certainly not very elegant (env:redirect_status is quite a hack), but it passes my modest tests. Unfortunately I can't test the www redirection, as I'm on localhost, and has no real access to a server, but that part should work too.</p> <p>You see, I used the ErrorDocument directive to specify the error page, and used the DirectorySlash Off request to make sure Apache doesn't interfere with the slash-appending fun. I also used the QSA (Query String Append) flag that, well, appends the query string to the request so that it's not lost. It looks kind of silly after the trailing slash, but anyhow.</p> <p>Otherwise it's pretty straightforward, and I think the comments explain it pretty well. Let me know if you run into any trouble with it.</p>
 

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