Forum Moderators: phranque
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST}!^www\.foo\.com [NC]
RewriteRule (.*) [foo.com...] [R=301,L]
RewriteRule ^archive_(.*)_(.*).html$ archive.php?y=$1&m=$2
RewriteRule ^(.*)_category.html$ category.php?catname=$1
What I am seeing is that if a user requests the page [foo.com...] they do not get a 404 but instead the page comes up ok and it is not a gif but instead it has the header and footer of my site. I would like to give the user a 404 instead. Any ideas on how I could modify my rewrite section to do this? An explanation of what and why it needs to be changed would be nice as I really want to learn how this works.
Thanks in advance.
There are two solutions, both of which are shown below. Pick one and delete or comment-out the other.
I also show some possible optimizations for your other rules -- Avoid using multiple .* subpatterns in rules whenever possible, as they lead to ambiguity and therefore to dramatically-increased execution time. These tweaks assume that only the number of undescores shown in your patterns will be present in the requested URLs -- These modified rules won't accept more than the specifified number of underscores. If this is not the case, the modified rules won't work, and will need to be modified further.
RewriteEngine on
RewriteBase /
#
# Redirect to fix additional directory paths appended to filenames (/logo.jpg/<additional_path>)
RewriteRule ^([^.]+\.[^/]+)/ http://www.example.com/$1 [R=301,L]
#
# Return 404-Not Found if additional directory paths appended to filenames (/logo.jpg/<additional_path>)
# (Force a 404 by rewriting to a filepath that does not exist)
RewriteRule ^([^.]+\.[^/]+)/ /nonexistent_file.lmth [L]
#
# Redirect non-canonical domain requests to canonical domain
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Rewrite static URLs to archive script
RewriteRule ^archive_([^_]+)_([^.]+)\.html$ archive.php?y=$1&m=$2 [L]
#
# Rewrite static URLs to category script
RewriteRule ^([^_]+)_category\.html$ category.php?catname=$1 [L]
[edited by: jdMorgan at 4:30 pm (utc) on Aug. 16, 2006]