Forum Moderators: phranque
I have been struggling with this for two days and I can't find the solution anywhere...I hope someone here can help me, please.
I am new to htaccess, so forgive me if it's a simple mistake.
I am using this for a website with a shopping cart, I have rewrite rules to write dynamic to static urls, non-www to www, and /index.html, etc to /.
I tried placing the errorDocument 404 /error.php (also tried with [mysite.php...] and it returns the standard page.
I tried to insert this code:
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) /error.php [L]
I am having a couple problems with this. The smaller issue is initially it was returning a 200 message, not a 404. Then I added header ("HTTP/1.1 404 Not Found"); and it got rid of the 200 message and returned the custom 404 page.
The big problem I'm having is using this rewrite rule, all of my product detail pages (that have been rewritten to static urls) are returning the error 404 page also, which obivously isn't good for an online store :)
I can't figure it out at all! :(
I really appreciate if someon can look at it and give me some suggestions.
Here is the entire htaccess code:
Options +Indexes
#<IfModule mod_php4.c>
#php_value session.use_only_cookies 1
#php_value session.use_trans_sid 0
#</IfModule>
# Index redirection
RewriteRule ^index\.(html?¦aspx?¦jspx?¦cgi¦pl¦php[2-5]¦cfm)$ http://www.example.com/ [R=301,L]
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
# Rewrite static URLs to dynamic
RewriteRule (.*)\.htm$ /proddetail.php?prod=$1
#
# Redirect only client-requested dynamic URLs to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)\ HTTP/
RewriteRule ^proddetail\.php$ http://www.example.com/%1.htm? [R=301,L]
AddHandler application/x-httpd-php .htm
AddHandler application/x-httpd-php .html
#RewriteCond %{REQUEST_FILENAME}!-f
#RewriteCond %{REQUEST_FILENAME}!-d
#RewriteRule (.*) /error.php [L]
<snip>
Thanks for your time!
Tony
[edited by: jdMorgan at 11:47 pm (utc) on Dec. 12, 2007]
[edit reason] No URLs, please. See Terms of Service. [/edit]
AddHandler application/x-httpd-php .htm
AddHandler application/x-httpd-php .html
#
#<IfModule mod_php4.c>
#php_value session.use_only_cookies 1
#php_value session.use_trans_sid 0
#</IfModule>
#
Options +Indexes +FollowSymLinks
RewriteEngine on
RewriteBase /
#
# Redirect only client-requested dynamic URLs to static
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /proddetail\.php\?prod=([^&]+)[^\ ]*\ HTTP/
RewriteRule ^proddetail\.php$ http://www.example.com/%1.htm? [R=301,L]
#
# Index redirection
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.(html?¦aspx?¦jspx?¦cgi¦pl¦php[2-5]¦cfm)[^\ ]*\ HTTP/
RewriteRule ^index\.(html?¦aspx?¦jspx?¦cgi¦pl¦php[2-5]¦cfm)$ http://www.example.com/ [R=301,L]
#
# Redirect requests for non-canonical domain
RewriteCond %{HTTP_HOST} ^example\.com(:[0-9]+)?$ [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
Rewrite all missing files except .htm to error.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [b]![/b]\.htm$ /error.php [L]
#
# Rewrite static .htm URLs to dynamic
RewriteRule ^([^.]+)\.htm$ /proddetail.php?prod=$1 [L]
Note the rule order: External redirects first, from most-specific to least-specific, followed by internal rewrites, again from most-specific to least specific. If two rules are mutually-exclusive, then order does not matter.
Replace all broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters. Completely flush your browser cache before testing any new code.
Jim
[edited by: jdMorgan at 12:06 am (utc) on Dec. 13, 2007]
Thank you so much for helping me with this and for taking the time to explain it! This works perfect, just like I needed it to...I really appreciate your help!
Can you tell me how this will handle (if it will at all) other errors besides 404? If it doesn't, is there a way I can add code handling for other errors, besides the errorDocument callout, which isn't working for me.
Also, should I leave the header ("HTTP/1.1 404 Not Found"); code in my error.php file?
These questions aren't as important to me, but I suppose I'll have to deal with them soon. Once again, thank you so much for your help!
Tony
Can you tell me how this will handle (if it will at all) other errors besides 404? If it doesn't, is there a way I can add code handling for other errors, besides the errorDocument callout, which isn't working for me.
Have you tried it since changing the code? ErrorDocuments for other errors should work now. The most likely reason the ErrorDocument 404 didn't work is that *none* of your product pages exist as 'real' files; They're handled by your proddetails script, but they do not exist as files. Using mod_rewrite allowed you to make this 404 handling conditional, to avoid this problem.
Note that you *must* specify a local URL-path (no "http:" and no domain name) for ErrorDocument directives; Otherwise the server will return a 302-Found status instead of the correct error status. This is documented in the Apache ErrorDocument directive documentation. The correct format is:
ErrorDocument 410 /page-is-gone.php
Also, you can use the "Live HTTP Headers" add-on for FireFox and Mozilla browsers to check all of your server's error responses.
I recommend using a plain static HTML page with no included objects of any kind for the 500-Server Error ErrorDocument if you do use a custom error document for that error. Otherwise, you introduce dependencies that may result in a server meltdown. For example, if you use PHP, and your PHP interpreter fails or gets corrupted, or if you inadvertently introduce a PHP configuration error, then handling the resulting 500-Server Error would cause another 500-Server Error, which in turn would cause yet another -- ad infinitum. In most cases, I recommend leaving the ugly but simple default Apache error page as the ErrorDocument for 500-Server Errors.
Be sure to create meaningful, helpful error pages that help your visitor find what they were looking for -- Do not just redirect all errors to the home page, as this invariably causes confusion (you'll see them trying the problematic URL again and again in your logs. Then they often leave.)
Also, should I leave the header ("HTTP/1.1 404 Not Found"); code in my error.php file?
Absolutely -- Otherwise, you'd return a 200-OK status for missing pages, which would be quite detrimental to your site's rankings.
Jim
Thanks for the great information. I will try the other error codes, as well as your other suggestions.
I have created my custom 404 page already and it offers solutions as well as an explanation.
I'll work on the others soon...
Thanks again for all of your help and advice! I really searched for most of two days without being able to find a good solution.
Thank you,
Tony