Note that there are some explanatory texts on larger screens.

plurals
  1. POmod rewrite to remove file extension, add trailing slash, remove www and redirect to 404 if no file/directory is available
    primarykey
    data
    text
    <p>I would like to create rewrite rules in my .htaccess file to do the following:</p> <ul> <li><p>When accessed via domain.com/abc.php: remove the file extension, append a trailing slash and load the abc.php file. url should look like this after rewrite: domain.com/abc/</p></li> <li><p>When accessed via domain.com/abc/: leave the url as is and load abc.php</p></li> <li><p>When accessed via domain.com/abc: append trailing slash and load abc.php. url should look like this after rewrite: domain.com/abc/</p></li> <li><p>Remove www</p></li> <li><p>Redirect to 404 page (404.php) when accessed url doesn't resolve to folder or file, e.g. when accessing either domain.com/nothingthere.php or domain.com/nothingthere/ or domain.com/nothingthere</p></li> <li><p>Make some permanent 301 redirects from old urls to new ones (e.g. domain.com/abc.html to domain.com/abc/)</p></li> </ul> <p>All php files sit in the document root directory, but if there is a solution that would make urls such as domain.com/abc/def/ (would load domain.com/abc/def.php) also work it would be great as well, but not necessary</p> <p>So here is what I have at the moment (thrown together from various sources and samples from around the web</p> <pre><code>&lt;IfModule mod_rewrite.c&gt; RewriteCond %{HTTPS} !=on # redirect from www to non-www RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] # remove php file extension RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{THE_REQUEST} ^GET\ /[^?\s]+\.php RewriteRule (.*)\.php$ /$1/ [L,R=301] # add trailing slash RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^.*[^/]$ /$0/ [L,R=301] # resolve urls to matching php files RewriteCond %{REQUEST_FILENAME} !-d RewriteRule (.*)/$ $1.php [L] </code></pre> <p></p> <p>With this the first four requirements seem to work, whether I enter domain.com/abc.php, domain.com/abc/ or domain.com/abc, the final url always ends up being domain.com/abc/ and domain.com/abc.php is loaded.</p> <p>When I enter a url that resolves to a file that doesn't exists I'm getting an error 310 (redirect loop), when really a 404 page should be loaded. Additionally I haven't tried if subfolders work, but as I said, that's low priority. I'm pretty sure I can just slap the permanent 301 redirects for legacy urls on top of that without any issues as well, just wanted to mention it. So the real issue is really the non working 404 page.</p>
    singulars
    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.
 

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