3

I created a 404.php page and added the following in my .htaccess file

ErrorDocument 404 /404.php

Also played around with removing the slash, adding a full URL, etc,

No matter what I do, I get my index.php UI regardless to what I write in the URL. Here is the thing, IT IS NOT re-directing me to domain.com/index.php or "/". The URL remains, but the UI is the index.php content

I'm adding my re-direct below in case you see something there that is conflicting

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
    RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

    RewriteCond %{SERVER_PORT} !^443$
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]


    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>
LOTUSMS
  • 10,317
  • 15
  • 71
  • 140

3 Answers3

3

From what I know of the mod_rewrite the last 4 lines are saying

That if the request filename is not a file nor a directory,

redirect to index.php.

And this is what is happening I believe.

All of the routes that are not available on the physical path in your application are going to index.php.

You will need some 404 mechanism in your application. If you are using some framework it usually has an exception like "RouteNotFound" thrown and then error handler redirects to a 404 page, you can also do something similar and redirect from within your application to 404.php.

if($should_be_an_error) {  
 header("Location: 404.php");
} 

(haven't used something like this for years but should be something similar)

or remove `

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

`

But then your other routes might stop working.

EDIT:

Since you wanted to redirect to 404.php instead of index.php(getting that from comments) the code should be changed to

RewriteRule . /404.php [L]

(last line of the rewrite block)

codisfy
  • 2,155
  • 2
  • 22
  • 32
  • This for some reason doesn't work for me. My css and js files don't load. It messes up with the paths I'm guessing. Plus also a weird console error about missing a `<` .... :/ But nonetheless, it didn't show my 404.php either – LOTUSMS Jul 24 '18 at 17:15
  • Fixed it. Your response got my muses to wake up lol As you said, if the requested url is not a file or directory go to.....index.php <-- Duh! I just changed that to 404.php and voila! Since you helped me figure it out, can you change your answer and I'll credit you – LOTUSMS Jul 24 '18 at 17:20
  • Actually, maybe you can help me with something else related to this. Now that it is successfully showing the 404.php, how can I re-write the URL to example/404.php instead of gibberish.php given that I type some gibberish in my domain? – LOTUSMS Jul 24 '18 at 17:23
  • 1
    then try removing [L] and add [R], it should redirect to 404.php from what I know – codisfy Jul 24 '18 at 17:26
  • Beautiful! That's what I wanted! Thanks man. I'm gonna open another question with another htaccess related question but not related to this one. If you want to check that out and help me out. – LOTUSMS Jul 24 '18 at 17:30
1

You can do this using FallbackResource directive. Remove all the code starting with RewriteBase / line and use this single line:

FallbackResource /404.php

This will fallback to /404.php for any 404 event.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Totally weird. Apache never returns 502 unless you have a proxy setup. – anubhava Jul 24 '18 at 19:02
  • A regular Host Gator linux hosting account. – LOTUSMS Jul 24 '18 at 19:04
  • It is difficult to guess. Check this official doc: https://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource This is all pretty standard – anubhava Jul 24 '18 at 19:07
  • I have another thread where somone is helpign me remove the .php fie extension and he has a different way to send to 404 as well. I'm liking his method, except the home page is also going to 404 now. But everything else works. https://stackoverflow.com/questions/51504638/removing-the-php-file-extension-from-the-url-request/51504767?noredirect=1#51504767 – LOTUSMS Jul 24 '18 at 19:09
-1

You can see this code below as an example if it match your requirement:

ErrorDocument 404 http://example.com/404/

RewriteCond %{REQUEST_URI} ^/404/$
RewriteRule ^(.*)$ /404.php [L]
Shahriat Hossain
  • 340
  • 4
  • 12