Link to home
Start Free TrialLog in
Avatar of Pete Winter
Pete WinterFlag for United Kingdom of Great Britain and Northern Ireland

asked on

htaccess RewriteRule stopping 404 page from working

See the code below in my htaccess file....


ErrorDocument 404 /404.php


#RewriteCond %{REQUEST_FILENAME} !-f

#RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*) /%1? [R=301,NC,L]

RewriteRule ^([a-z0-9-]+)/?$ /pages/index.php?link=$1 [NC]


I am not getting redirected to the 404 page. What is wrong here. Is it to do with he rewrite rule I have set up for my dynamic pages? How can I fix?

Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Well, your first rewrite rule is basically redirecting every request, so there's no chance of you hitting 404. If you can't get a 404, you aren't going to be redirected to your 404 document.


So I guess you need to elaborate on what you're trying to do.


EDIT: I took another look at your rule now that I'm not on my phone. That first rule looks like it would create an infinite loop, actually. Also, you're using %1, which I'm guessing you wanted to be $1...? It seems like there might be several problems here. Also, I don't see the RewriteEngine On directive, so unless you have that somewhere, your RewriteRule rules aren't going to even run.

a 404 is an error page.
so use the errordocument directive
How are you testing it?
Avatar of Pete Winter

ASKER

Thanks for the replies.

I do have "RewriteEngine On" at the top of the code. I just didn't share that part. I have worked out where my problem is. Please ignore the code above and look at the below...

RewriteEngine On

ErrorDocument 404 /404.php

RewriteRule ^([a-z0-9-]+)/?$ /pages/index.php?link=$1 [NC]

Open in new window


I understand that anything after the domain.com/xxxx will use the pages dynamic page, but how can this work with the 404 error. Within the /pages/index.php I check if the url is in the database and if not redirect to a 404 page, but this doesn't seem to count as a proper 404 error as far as I understand?

As an example of what I am doing see the example php code below in the /pages/index.php
if($pages_total_rows < 1) {
        header( 'Location: /404.php' ) ;
}

Open in new window


Hope that makes sense? What am I doing wrong?
ErrorDocument 404 /404.php should probably be the first thing in the file.  However... that is an instruction to the Web server when it encounters a page that is not found.  If you load the index page and don't find it in your database, you will have caused a 200 response (index page was found and loaded) and Not a 404 response.  Note also that the 'header' command must be executed Before any content is sent back to the user or it will not execute at all.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial