homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Custom 404 status code help - htaccess help

 1:27 pm on Mar 2, 2011 (gmt 0)


I created a thread related to this problem here: [webmasterworld.com...]

I have a problem with my custom 404 page not giving the correct header, it is not giving a 404 error but rather a 302.

It seems to me that the issue is to do with my htaccess file and a condition that I have in it that is stopping the 404 working correctly but i have no idea which one it could be.

here is my htaccess file, sorry for posting a large htaccess file but I need guidance as to what part of it is incorrect :)

any ideas?
thank you

mod_gzip_on Yes
mod_gzip_dechunk Yes
mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
mod_gzip_item_include handler ^cgi-script$
mod_gzip_item_include mime ^text/.*
mod_gzip_item_include mime ^application/x-javascript.*
mod_gzip_item_exclude mime ^image/.*
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*

#Prevent timeouts during upload (May require editing of php.ini)
php_value upload_max_filesize 800M
php_value post_max_size 800M
php_value max_execution_time 1600
php_value max_input_time 1600
php_value register_globals 0
ErrorDocument 404 /404.php

RewriteEngine on

#Moved Codes
RewriteRule ^category/$ example.com/category [R=301,L]
RewriteRule ^tag/$ example.com/tags [R=301,L]
RewriteRule ^popular_tags/$ example.com/popular_tags [R=301,L]
RewriteRule ^webmasters/$ example.com/about [R=301,L]
RewriteRule ^contact/$ example.com/about [R=301,L]
RewriteRule ^about/$ example.com/about [R=301,L]
RewriteRule ^subscriptions/$ example.com/subscriptions [R=301,L]
RewriteRule ^favorites/$ example.com/favorites [R=301,L]
RewriteRule ^disabled/$ example.com/disabled [R=301,L]
RewriteRule ^search-stats/$ example.com/search-stats [R=301,L]
RewriteRule ^channel-editor/$ example.com/channel-editor [R=301,L]
RewriteRule ^seo-tools/$ example.com/seo-tools [R=301,L]
RewriteRule ^title-editor/$ example.com/title-editor [R=301,L]
RewriteRule ^import-failed/$ example.com/import-failed [R=301,L]
RewriteRule ^import-queued/$ example.com/import-queued [R=301,L]
RewriteRule ^queued/$ example.com/queued [R=301,L]
RewriteRule ^most_recent/$ example.com/new [R=301,L]
RewriteRule ^most_viewed/$ example.com/most_viewed [R=301,L]
RewriteRule ^most_discussed/$ example.com/most_discussed [R=301,L]
RewriteRule ^top_rated/$ example.com/top_rated [R=301,L]
RewriteRule ^featured/$ example.com/featured [R=301,L]
RewriteRule ^directory/$ example.com/directory [R=301,L]
RewriteRule ^search/$ example.com/search [R=301,L]
RewriteRule ^logout/$ example.com/logout [R=301,L]
RewriteRule ^login/$ example.com/login [R=301,L]
RewriteRule ^lostpw/$ example.com/lostpw [R=301,L]
RewriteRule ^register/$ example.com/register [R=301,L]
#Content Area (Relabeled to /channel - previous label kept for smooth upgrade path for beta users - 301 redirect)
RewriteRule ^channel/([^/.]+)$ example.com/$1 [R=301,L]
RewriteRule ^channel/([^/.]+)/page([^/.]+)$ example.com/$1/page$2 [R=301,L]
RewriteRule ^channel/([^/.]+)/([^/.]+)$ example.com/$1/$2 [R=301,L]
RewriteRule ^channel/([^/.]+)/([^/.]+)/page([^/.]+)$ example.com/$1/$2/page$3 [R=301,L]
RewriteRule ^channel/([^/.]+)/([^/.]+)/([^/.]+)$ example.com/$1/$2/$3 [R=301,L]
RewriteRule ^channel/([^/.]+)/([^/.]+)/([^/.]+)/page([^/.]+)$ example.com/$1/$2/$3/page$4 [R=301,L]
RewriteRule ^channel/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)$ example.com/$1/$2/$3/$4 [R=301,L]

RewriteRule ^user/([^/.]+)$ example.com/$1 [R=301,L]
RewriteRule ^user/([^/.]+)/page([^/.]+)$ example.com/$1/page$2 [R=301,L]
RewriteRule ^user/([^/.]+)/([^/.]+)$ example.com/$1/$2 [R=301,L]
RewriteRule ^user/([^/.]+)/([^/.]+)/page([^/.]+)$ example.com/$1/$2/page$3 [R=301,L]
RewriteRule ^user/([^/.]+)/([^/.]+)/([^/.]+)$ example.com/$1/$2/$3 [R=301,L]
RewriteRule ^user/([^/.]+)/([^/.]+)/([^/.]+)/page([^/.]+)$ example.com/$1/$2/$3/page$4 [R=301,L]
RewriteRule ^user/([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)$ example.com/$1/$2/$3/$4 [R=301,L]
RewriteRule ^stats/$ example.com/stats [R=301,L]
RewriteRule ^blog_drafts/$ example.com/blog_drafts [R=301,L]
RewriteRule ^blog_queue/$ example.com/blog_queue [R=301,L]
RewriteRule ^blog/$ example.com/blog [R=301,L]
RewriteRule ^profile/$ example.com/profile [R=301,L]

RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ example.com/$1 [R=301,L]

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ example.com/$1 [L,R=301]


RewriteRule ^profile/([^/.]+)$ index.php?profile=TRUE&m=$1 [L]
RewriteRule ^profile$ index.php?profile=TRUE [L]


RewriteRule ^blog/page([^/.]+)$ index.php?m=b&page=$1 [L]
RewriteRule ^blog/([^/.]+)$ index.php?m=b&bid=$1 [L]
RewriteRule ^blog$ index.php?m=b [L]

#Blog Queue

RewriteRule ^blog_queue/page([^/.]+)$ index.php?m=b_q&page=$1 [L]
RewriteRule ^blog_queue/([^/.]+)$ index.php?m=b_q&bid=$1 [L]
RewriteRule ^blog_queue$ index.php?m=b_q [L]

#Blog Drafts

RewriteRule ^blog_drafts/page([^/.]+)$ index.php?m=b_d&page=$1 [L]
RewriteRule ^blog_drafts/([^/.]+)$ index.php?m=b_d&bid=$1 [L]
RewriteRule ^blog_drafts$ index.php?m=b_d [L]


RewriteRule ^stats$ index.php?stats=TRUE [L]

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

#Content Areas (user kept for beta testers)
RewriteRule ^thumb/([^/.]+)$ /inc/thumbs.php?id=$1 [L]


RewriteRule ^register$ index.php?register=TRUE [L]
RewriteRule ^registration/([^/.]+)$ index.php?registration=TRUE&un=$1 [L]
RewriteRule ^registration/([^/.]+)/([^/.]+)$ index.php?registration=TRUE&un=$1&verify=$2 [L]

#Password Reminder

RewriteRule ^lostpw$ index.php?lostpw=TRUE [L]


RewriteRule ^login$ index.php?login=TRUE [L]


RewriteRule ^logout$ index.php?logout=TRUE [L]


RewriteRule ^search/page([^/.]+)$ index.php?search=1&page=$1 [L]
RewriteRule ^search$ index.php?search=1 [L]


RewriteRule ^directory/([^/.]+)$ index.php?m=dir&s=$1 [L]
RewriteRule ^directory/([^/.]+)/page([^/.]+)$ index.php?m=dir&s=$1&page=$2 [L]


RewriteRule ^featured/page([^/.]+)$ index.php?m=e&page=$1 [L]
RewriteRule ^featured$ index.php?m=e [L]

#Top Rated

RewriteRule ^top_rated/page([^/.]+)$ index.php?m=h&page=$1 [L]
RewriteRule ^top_rated$ index.php?m=h [L]

#Most Discussed

RewriteRule ^most_discussed/page([^/.]+)$ index.php?m=d&page=$1 [L]
RewriteRule ^most_discussed$ index.php?m=d [L]

#Most Viewed

RewriteRule ^most_viewed/page([^/.]+)$ index.php?m=m&page=$1 [L]
RewriteRule ^most_viewed$ index.php?m=m [L]

#Most Recent

RewriteRule ^most_recent/page([^/.]+)$ index.php?m=n&page=$1 [L]
RewriteRule ^most_recent$ index.php?m=n [L]


RewriteRule ^queued/page([^/.]+)$ index.php?m=a_q&page=$1 [L]
RewriteRule ^queued$ index.php?m=a_q [L]

#Import Queued

RewriteRule ^import-queued/page([^/.]+)$ index.php?m=i_q&page=$1 [L]
RewriteRule ^import-queued$ index.php?m=i_q [L]

#Import Failed

RewriteRule ^import-failed/page([^/.]+)$ index.php?m=i_f&page=$1 [L]
RewriteRule ^import-failed$ index.php?m=i_f [L]

#Title Editor

RewriteRule ^title-editor$ index.php?m=ti [L]

#SEO Tools

RewriteRule ^seo-tools$ index.php?m=seo [L]

#Channel Editor

RewriteRule ^channel-editor$ index.php?m=ce [L]

#Search Stats

RewriteRule ^search-stats/page([^/.]+)$ index.php?m=ss&page=$1 [L]
RewriteRule ^search-stats$ index.php?m=ss [L]


RewriteRule ^disabled/page([^/.]+)$ index.php?m=a_d&page=$1 [L]
RewriteRule ^disabled$ index.php?m=a_d [L]


RewriteRule ^favorites/page([^/.]+)$ index.php?m=f&page=$1 [L]
RewriteRule ^favorites$ index.php?m=f [L]


RewriteRule ^subscriptions/page([^/.]+)$ index.php?m=s&page=$1 [L]
RewriteRule ^subscriptions$ index.php?m=s [L]


RewriteRule ^about$ about.php [L]


RewriteRule ^contact$ index.php [L]


RewriteRule ^webmasters$ index.php [L]

#Popular Tags

RewriteRule ^popular_tags/([^/.]+)$ index.php?m=p&m=$1 [L]
RewriteRule ^popular_tags$ index.php?m=p [L]


RewriteRule ^tag/([^/.]+)$ index.php?tag=$1 [L]
RewriteRule ^tag/([^/.]+)/page([^/.]+)$ index.php?tag=$1&page=$2 [L]


RewriteRule ^category/([^/.]+)$ index.php?cat=1&scat=$1 [L]
RewriteRule ^category/([^/.]+)/page([^/.]+)$ index.php?cat=1&scat=$1&page=$2 [L]
RewriteRule ^category$ index.php?cat=1 [L]

#Group List
RewriteRule ^girls/([^/.]+)$ index.php?m=girls&s=$1 [L]

#Content Area
RewriteRule ^([^/.]+)$ index.php?uid=$1 [L]
RewriteRule ^([^/.]+)/page([^/.]+)$ index.php?uid=$1&page=$2 [L]
RewriteRule ^([^/.]+)/([^/.]+)$ index.php?uid=$1&pid=$2&gid=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/page([^/.]+)$ index.php?uid=$1&pid=$2&page=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)$ index.php?uid=$1&pid=$2&gid=$3 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/page([^/.]+)$ index.php?uid=$1&pid=$2&gid=$3&page=$4 [L]
RewriteRule ^([^/.]+)/([^/.]+)/([^/.]+)/([^/.]+)$ index.php?uid=$1&pid=$2&gid=$3&iid=$4 [L]

# ?m=h&page=4/page1077/page5/page1116/page10/page1129/page7/page8 fixer
RewriteCond %{THE_REQUEST} m=h&page=([0-9]+)
RewriteRule ^(.*)$ example.com/top_rated/page%1? [R=301,L]

RewriteCond %{THE_REQUEST} m=m&page=([0-9]+)
RewriteRule ^(.*)$ example.com/most_viewed/page%1? [R=301,L]

RewriteCond %{THE_REQUEST} m=n&page=([0-9]+)
RewriteRule ^(.*)$ example.com/most_recent/page%1? [R=301,L]

RewriteCond %{THE_REQUEST} m=d&page=([0-9]+)
RewriteRule ^(.*)$ example.com/most_discussed/page%1? [R=301,L]

RewriteCond %{THE_REQUEST} m=e&page=([0-9]+)
RewriteRule ^(.*)$ example.com/featured/page%1? [R=301,L]



 10:16 pm on Mar 2, 2011 (gmt 0)

You must list all the 301 redirects before any of the rewrites. You have a bunch of redirects at the end that should be near the beginning.

The redirects can be much simplified, the first nine condensing to:

RewriteRule ^(category|popular_tags|subscriptions|favorites|disabled)/$ http://example.com/$1 [R=301,L]
RewriteRule ^tags?/$ http://example.com/tags [R=301,L]
RewriteRule ^(webmasters|contact|about)/$ http://example.com/about [R=301,L]


 11:29 am on Mar 3, 2011 (gmt 0)

Hey g1smd,

thanks for the advice and I will look into those changes.

does anyone know about the specific 404 error i need help with?



 1:33 pm on Mar 3, 2011 (gmt 0)

g1smd is giving you a guide on simplifying your rules, and not doing it for you. With a set of rules as complicated as you have, it's no wonder you're lost as to where it's falling off the wheels.

However... Can you give an example of a URI that should 404 but ends up with a 302?


 2:54 pm on Mar 3, 2011 (gmt 0)


an example is:

gets the 302 redirect.

gets the correct 404 header.



 8:12 pm on Mar 3, 2011 (gmt 0)

gets the 302 redirect.

That URL request will be matched by this pattern:

RewriteRule ^([^/.]+)/([^/.]+)$ index.php?uid=$1&pid=$2&gid=$3 [L]

and will be rewritten to:


Your script should be checking that $1, $2 and $3 are valid values and then constructing the correct 404 header and error message HTML page if they are not.

One problem is that $3 is always blank. What your script does with that malformed request is anyone's guess, but may be linked to the problem you are having now.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved