0

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.

Edited by dyingatmidnight: edititng code

2
Contributors
2
Replies
3
Views
4 Years
Discussion Span
Last Post by dyingatmidnight
0

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.

Edited by blocblue

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.