Forum Moderators: phranque

Message Too Old, No Replies

weird 301 redirect issue

         

newwebprob

4:04 am on Jan 31, 2009 (gmt 0)

10+ Year Member



I'm having a weird issue with a 301 redirect for non www to www
Apache server
here is my code in .htaccess

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com$ [NC]
rewriterule ^(.*)$ http://www.example.com/$1 [L,R=301]

also tried

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

Site background
I have a landing page at www.example.com (in the public_html root) with an enter link to the main site which is an oscommerce site in a folder "catalog" also in public_html folder

Both versions of the redirect work as expected in Firefox, however in Internet Explorer the redirect works but none of my CSS menues or site graphics show up just text.

Anyone have a clue what IE is doing different from Firefox or a way to solve the issue?

Any help is greatly appreciated!

[edited by: tedster at 4:12 am (utc) on Jan. 31, 2009]
[edit reason] switch to example.com - it can never be owned [/edit]

jdMorgan

2:32 pm on Jan 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Did you completely flush (or disable) the browser caches before testing?

If not, either browser may serve you stale, outdated pages or server responses until the cache entries expire or are replaced by newer ones.

As a side question, do you use additional domains or subdomains other than "www" on this site? The rules you posted above will redirect non-www to www, but they won't redirect "junk.example.com" to www. Neither will they redirect any www or non-www requests if there is an appended port number of FQDN token -- both of which are legal and can happen.

Jim

[edited by: jdMorgan at 8:27 pm (utc) on Jan. 31, 2009]

g1smd

6:23 pm on Jan 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Check the Capitalisation of words like RewriteCond and HTTP_HOST and make sure you follow the exact style as outlined in the examples in the Apache documentation.

newwebprob

12:49 am on Feb 1, 2009 (gmt 0)

10+ Year Member



Thanks for the reply!
I have cleared the cache, as well as tried from another computer(New one, first time on the net)the same issue IE7 will not display the page correctly(no pictures, CSS menues etc, only text)Mozilla works as expected.

There are no additional domains only the 1.

Also retried the syntax of RewriteCondition vs what I had as rewritecondition same issue.

As soon as I replace .htaccess with the original(exactly the same but without the redirect) everthing returns to normal and works correctly.
I think the fact that the pics and CSS are not working in IE may be some type of good clue(I can't figure it out though) we use a CSS for the main page located in the /public_html and another CSS for the oscommerce .php site located at /public_html/catalog, Firefox handles the redirect and everything else fine..

A little stumped here, this should work in IE?

again here is what I have tried to date.

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com$ [NC]
rewriterule ^(.*)$ http://www.example.com/$1 [L,R=301]

also tried

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

and

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

jdMorgan

1:23 am on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Since a redirect such as you have here terminates the current HTTP transaction and informs the browser that it should start a new HTTP transaction and re-request the originally-requested resource at a new URL, and the only difference in the new URL here is the www subdomain, there's no reason that Firefox should work with the "www" subdomain, and MSIE should not. It basically makes no sense.

Unfortunately, there isn't an MSIE browser add-on like "Live HTTP Headers" for Firefox, so you can't check the HTTP request headers that MSIE is with its requests for the included image and CSS files, etc. that don't seem to be working. So, all I can recommend is to look at *both* your server access log and your server error log, and see if you can spot some difference in what each browser is requesting.

We see this kind of problem with *all* browsers when people are using page-relative links to included objects and then install internal rewrites which change the effective "directory-level" of their URLs. But the cause of this problem does not exist when an external redirect is used, and you're only seeing it with one browser. So this is a really strange problem.

You might want to carefully review the rest of your .htaccess file(s) and make sure there are no other directives involved with image and CSS delivery which also have HTTP_HOST dependencies.

Jim

newwebprob

1:28 am on Feb 1, 2009 (gmt 0)

10+ Year Member



OK have it working now!

Heres what did the trick, not quite sure why this syntax worked and the others didn't maybe someone could shed some light, I'm of the "you cangive a man a fish and he'll eat for a day" "you can teach a man to fish and he'll eat for a lifetime" frame of mind and would like to understand this.

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^mysite.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) [mysite.com...] [L,R]

Previous tries for comp.

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com$ [NC]
rewriterule ^(.*)$ http://www.example.com/$1 [L,R=301]
>>>>>>>>>>>
Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]
>>>>>>>>>>
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{http_host} ^example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

newwebprob

1:55 am on Feb 1, 2009 (gmt 0)

10+ Year Member



OK the redirect from the main landing page to the main catalog page is working in both IE and Firefox now showing .www with all images and all CSS attributes being displayed as intended, and even stranger the main .php page (public_html/catalog shows the desired [mysite.com...] but if I try [mysite.com...] it does not add the .www so once inside the site(public_html/catalog)the redirect does not function, also all the interrior pages of oscommerce still show [mysite.com...] instead of the desired [mysite.com...] (arghhh)as I mentioned there are .htaccess files in both the main public_html and public_html/catalog, I tried adding the same redirect code that eventually worked for the main landing page to the .htaccess in public_html/catalog but no luck there, posibly there is a .php specific way to do this?

g1smd

2:03 am on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That code shouldn't work in .htaccess for multiple reasons, not least because of the leading slash in the pattern path, and the mismatch of "not mysite" redirecting to "www.mysite".

In fact, fix the leading slash problem and the code will generate an infinite loop due to the other problem, unless you change the condition to "not www.mysite" instead.

You're still using random capitalisation of the reserved words in your rules. That always makes me uneasy.

newwebprob

4:20 am on Feb 1, 2009 (gmt 0)

10+ Year Member



Would you have a sample of a known working correct 301 redirect I could experiment with?

Wayder

7:30 am on Feb 1, 2009 (gmt 0)

10+ Year Member Top Contributors Of The Month



I use the following.

## REDIRECT NON-WWW TO WWW
RewriteCond %{HTTP_HOST} !^w{3}\. [NC]
RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]

Ray...

g1smd

4:56 pm on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note that
^(.*)$
simplifies to
(.*)

newwebprob

7:28 pm on Feb 1, 2009 (gmt 0)

10+ Year Member



That works for the non www to www for all pages in the site, but it gives me the same issue where the CSS style is not applied(like its not being seen at all)and .php pages show as only text, although now my product images show up, but no other images such as the header logo or footer logo.
It seems like anything that is in the public_html/catalog folder(.php side of the site, also has it own .htaccess example below)gets screwed up.
Not sure why this would happen, if I type in the full address (with the orig non modded .htaccess in place) [mysite.com...] it shows fine and everything works as expected, I'm assuming the mod simply enforces the www and does not care what the end is (ie: .htm, .php etc)

# $Id: .htaccess 1739 2007-12-20 00:52:16Z hpdl $
#
# This is used with Apache WebServers
#
# For this to work, you must include the parameter 'Options' to
# the AllowOverride configuration
#
# Example:
#
# <Directory "/usr/local/apache/htdocs">
# AllowOverride Options
# </Directory>
#
# 'All' with also work. (This configuration is in the
# apache/conf/httpd.conf file)

# The following makes adjustments to the SSL protocol for Internet
# Explorer browsers

#<IfModule mod_setenvif.c>
# <IfDefine SSL>
# SetEnvIf User-Agent ".*MSIE.*" \
# nokeepalive ssl-unclean-shutdown \
# downgrade-1.0 force-response-1.0
# </IfDefine>
#</IfModule>

# If Search Engine Friendly URLs do not work, try enabling the
# following Apache configuration parameter

# AcceptPathInfo On

# Fix certain PHP values
# (commented out by default to prevent errors occuring on certain
# servers)

# php_value session.use_trans_sid 0
# php_value register_globals 1

jdMorgan

7:49 pm on Feb 1, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I didn't comment on the code you initially posted, because it should have worked fine, the upper/lowercase issues notwithstanding.

You should enable that "downgrade" code for MSIE that is commented-out above, though. I'm not sure if it will affect this problem or not. But because this is an external redirect, I strongly feel that this is a client-side problem, and not a server-side problem.

Jim

newwebprob

1:25 am on Feb 2, 2009 (gmt 0)

10+ Year Member



OK finally figured it out with all your great help!
Of course it ended up being an issue with oscommerce..

In case any poor unfortunate person runs into this issue with 301 redirects working with oscommerce here is the fix.

I used waiders sample redirect

Options+FollowSymlinks
RewriteCond %{HTTP_HOST} !^w{3}\. [NC]
RewriteRule ^(.*)$ [%{HTTP_HOST}...] [R=301,L]

although I have now tried it with many of the first ones I used and all are OK.

The key is to mod 2 files in oscommerce

#1 very important if you want to be able to access your admin section once the redirect is in place(so do this first!)
catalog/admin/includes/configure.php
Make sure the first 3 lines include the www(by default oscommerce only uses [mysite.com...] for both the admin and catalog configure.php)

define('HTTP_SERVER', 'http://www.mysite.com');
define('HTTP_CATALOG_SERVER', 'http://www.mysite.com');
define('HTTPS_CATALOG_SERVER', 'http://www.mysite.com');

Then in catalog/admin/includes/configure.php
Same thing make sure the first 2 lines include the www.

define('HTTP_SERVER', 'http://www.mysite.com');
define('HTTPS_SERVER', 'http://www.mysite.com');

Thats it and everything will function as it should!

Thanks again for all your help, this is an awesome forum!

g1smd

2:08 am on Feb 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note that
^(.*)$
simplifies to
(.*)
here.

jdMorgan

2:26 am on Feb 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's a missing space in that code, and the "!^w{3}\." pattern really isn't sufficient. It can be 'fooled' by many, many non-canonical hostnames, and should only be used when you cannot explicitly handle all of the domains. It's also unnecessarily obscure -- "!^www\." would be a bit more straightforward, and use of [NC] on the line is logically incorrect.

I'd strongly suggest:


Options +FollowSymLinks
RewriteEngine on
#
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

for all single-domain applications. Require an exact match, no appended FQDN indicator or port numbers, and do not accept case errors.

See further explanatory comments in this concurrent thread: [webmasterworld.com...]

Jim

[edited by: jdMorgan at 3:29 am (utc) on Feb. 2, 2009]