Forum Moderators: phranque

Message Too Old, No Replies

non-www redirect to www does not work on pages in the cgi-bin

         

proboscis

2:49 am on Mar 21, 2009 (gmt 0)

10+ Year Member



I redirect non-www pages to www with htaccess. It works but when I try to access a non-www page from within the cgi-bin and that page does not exist instead of redirecting to the www version with a server header of 404 it does a 301 redirect to my custom 404 page with a server header 304 FOUND.

So how do I make non-www pages that are within the cgi-bin return a 404 if they do not exist?

If I take the www redirect out, I get the 404 so can I do it by making my www redirect not apply to pages within the cgi-bin? Something like that...or?

This is all I have now:

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

Thank you!

jdMorgan

3:28 am on Mar 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you set up this cgi-bin directory, or is it something your host set up?

A server response header of 304 is not "Found" but rather, "Not Modified", unless you meant you got a "302-Found" instead of "304-Not Modified" (these details deserve a lot of care, since they can greatly affect the diagnosis).

You can exclude your cgi-bin files from the "www" redirect using a RewriteCond with a negative-match pattern on %{REQUEST_URI}, e.g.


RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\. [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\:[0-9]+
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Jim

proboscis

5:00 am on Mar 21, 2009 (gmt 0)

10+ Year Member



Hi Jim,

Oops, you're right I meant 304 not modified.

I did not set up the cgi-bin directory, it's set up by my host.

Okay, I tried the code that you gave me (thank you!) and it works as far as accessing a non-www page that is there.

But if I try to get a non-www page that is not there it still does a 301 to my 404 page.

If I try to get a page that is not there but WITH the www everything works fine.

If I take out the www redirect code from my htaccess file, everything works fine.

And I see I made another mistake with what I said the first time, there is actually more in my htaccess file. It looks like this:

# Remove multiple slashes anywhere in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . http://www.example.com%1/%2 [R=301,L]
#
# Remove trailing slash if filetype present in URL
RewriteRule ^(.+\.[^/]+)/$ http://www.example.com/$1 [R=301,L]
# Remove extra URL-path info if filetype present in URL
RewriteRule ^([^.]+\.[^/]+)/ http://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^/cgi-bin/
RewriteCond %{HTTP_HOST} ^example\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\. [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com\:[0-9]+
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

ErrorDocument 404 /404.html
#

Any ideas?

jdMorgan

5:21 pm on Mar 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try changing

RewriteCond %{REQUEST_URI} !^/cgi-bin/

to

RewriteCond %{REQUEST_URI} !^/404\.html$

Jim

proboscis

7:11 pm on Mar 21, 2009 (gmt 0)

10+ Year Member



I think it's really working.

Can't thank you enough :)!