Score:1

ErrorDocument and extension less links not working together

cn flag

On my website, I have the following code to omit the extension of php pages

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

I also added this code to show custom 404 pages

ErrorDocument 404 /404

Both of these work independently but not when used together. When I visit a dummy link like www.mydomain.com/pagethatdontexist, I want to see my 404 page, instead I see File not found error which is I guess coming from the server, so I'm getting a 404 on 404 apparently.

I've tried moving ErrorDocument above and below extension remover code, tried /404.php as well. I also tried creating an exception for 404 like

RewriteCond %{REQUEST_URI} !^/404.php$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Nothing works.

Edit: When extension code is removed and I visit www.mydomain.com/pagethatdontexist I see my 404 page. But if I visit www.mydomain.com/pagethatdontexist.php I see bland File not Found error from the server. Maybe it can give a clue as to what is going on.

Score:0
cn flag

I think your regex doesn't match what you want. With the following i'm able to handle url like :

  • /mypage
  • /mypage/myid

The regex :

    RewriteEngine On # Activer le module Rewrite
    RewriteRule ^/(\w+)\/?$ /$1.php [NC,L]
    RewriteRule ^/(\w+)\/(\d+)$ /$1.php?id=$2
Score:0
kz flag

My guess is that you are on a LiteSpeed server, not Apache? This should "work" on Apache, but not LiteSpeed because of the way server variables are not updated between internal requests.

However, there is no need (and you should not) omit the file extension on the ErrorDocument directive itself. Extensionless URLs are "cosmetic" for users. The ErrorDocument is entirely hidden from users. But, if you omit the file extension on the ErrorDocument directive then it would trigger an additional internal rewrite, after the internal subrequest for the error document - which should be avoided.

So, you should include the file extension in the ErrorDocument directive:

ErrorDocument 404 /404.php
Whip avatar
cn flag
I am using Apache. It should and it does work on xampp but not on my server. Looks like `ErrorDocument` is ignored when extension remover code is present. Aren't these two completely separate directives? What could be interfering here?
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.