Hi there, I'm currently rewriting my dynamic urls using mod_rewrite which is working fine. I'm just wondering how best to handle pages that don't exist. Right now it works by getting the page id and displaying the content, if the pageid doesn't exist it displays a custom 404 message. What I would like to do is change the url so that it displays a generic 404 url instead of the not found one. This is my htaccess file:

RewriteEngine on

Options -Indexes
RewriteBase /

# Disable rewrite for specific directories 
RewriteCond %{REQUEST_URI} !^/admin/
# Disable rewrite for specific directories 
RewriteCond %{REQUEST_URI} !^/portal/
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^([^/\.]+)/?$ index.php?id=$1 [L]

# Disable rewrite for specific directories 
RewriteCond %{REQUEST_URI} !^/admin/
# Disable rewrite for specific directories 
RewriteCond %{REQUEST_URI} !^/portal/
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?id=$1&code=$2 [L]

RewriteRule ^admin/([^/\.]+)/?$ admin/error.php?err=3 [R=301,L]

So when the $_GET["id"] query returns a rowCount of 0 from the database it sets the pageid to 404 and pulls the 404 message from the database. But the url is still the non-existant page and I'd like to change that if possible. I'm just not sure how.

Recommended Answers

All 2 Replies

In short, you can't do a redirect and return a 404. To change the URL, you'll be doing a redirect, which will return a status code of 301 or 302. This could be considered a soft 404, rather than a hard 404.

I believe that Web crawlers however require a hard 404 to correctly recognise that page is missing or doesn't exist.

Thanks for the help. I think the 'soft 404' will work right now, if it becomes an issues then I look into another solution.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.