homepage Welcome to WebmasterWorld Guest from 54.196.196.108
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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

Apache Web Server Forum

    
.htaccess non-www redirect not working after wordpress install
Variable




msg:4394548
 9:38 pm on Dec 5, 2011 (gmt 0)

Hi Everyone,

I was redirecting the non-www version of my site "example.com" to the www version "www.example.com" using mod_rewrite in .htaccess.

I then installed wordpress into a /blog/ subfolder but have it running as if it was installed in the root folder. So when someone goes to "www.example.com" they get the main blog index page.

My problem is that when someone types in "example.com" they now get a "Internet explorer cannot display the webpage" error. I used a check server header tool and it shows that I am getting a 301 redirect, but it does not list an address as the new destination anymore.

Here is my .htaccess:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

I am using permalinks in wordpress with /blog/%postname%/ as the structure and http://www.example.com/blog as the wordpress URL and http://www.example.com as the site URL.

Every URL that does not have a www 301 redirects perfectly to the www version except for the root example.com URL. For example:

example.com/blog/my-blog-post/

does a 301 redir to

www.example.com/blog/my-blog-post/

but example.com brings up an error page and www.example.com brings up the blog home page as expected.

I know this post references wordpress heavily but since the problem is most likely in my .htaccess file I felt it was appropriate to post it in this forum.

Any help anyone can offer would be immensely appreciated.

Variable

 

g1smd




msg:4394564
 10:36 pm on Dec 5, 2011 (gmt 0)

Use the Live HTTP Headers extension for Firefox to check exactly what does happen.

The code above should redirect root requests to www.

The redirect code isn't optimum, the RewriteCond pattern should be
!^(www\.example\.com)?$
lucy24




msg:4394577
 11:09 pm on Dec 5, 2011 (gmt 0)

Is that your original htaccess or the version that came with WordPress?

Where is the htaccess physically located? Are you sure WordPress didn't sneak in and install a new htaccess (either in its own directory or overwriting the existing htaccess) of its own? I believe that's standard WordPress behavior, and the <if mod...> parts definitely look like WP boilerplate.

When you say subfolder do you mean

/userspace/www.example.com/blog/
or
/userspace/blog/

?

Is the WordPress redirect-- the one that sends www.example.com requests to the WP front page-- happening via your htaccess, something the host is doing, or top-level DNS?

Variable




msg:4394583
 12:00 am on Dec 6, 2011 (gmt 0)

Is that your original htaccess or the version that came with WordPress?


It's a combination of both. The code below "RewriteBase /" was generated with WordPress. The two lines doing the 301 redirect were added by me almost verbatim from my old .htaccess.

Where is the htaccess physically located? Are you sure WordPress didn't sneak in and install a new htaccess


The .htaccess is located in the root of the folder that handles requests for the domain "example.com". The /blog/ subfolder is a subfolder directly off the root "example.com/blog/". I checked the /blog/ subfolder and found another .htaccess with the following in it:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>

Use the Live HTTP Headers extension for Firefox to check exactly what does happen. The code above should redirect root requests to www.


I installed firefox and tried to get the live http headers extension for it but it doesn't work for the latest version of firefox. Is there a way to get an old version?

As mentioned earlier, every url on my site is 301 redirecting the non-www to the www version as I want. The problem is only with "example.com".

Thanks all for your help so far. It is very much appreciated.

Variable




msg:4394597
 12:55 am on Dec 6, 2011 (gmt 0)

I installed firefox and tried to get the live http headers extension for it but it doesn't work for the latest version of firefox. Is there a way to get an old version?


Ok...I found firefox 4.0 on some site and installed the live http headers addon. When I go to "example.com" in firefox 4.0 it 301 redirects me to "www.example.com".

In IE and Chrome I still get an error for "example.com".

lucy24




msg:4394605
 1:21 am on Dec 6, 2011 (gmt 0)

What the ###?

If the redirect is happening within your own htaccess, there should be absolutely no difference in behavior between browsers. The "Live Headers" extension is just informational; it doesn't do anything. Is there any way this could be a DNS issue?

What error message do you get in Chrome? Do other people on webkit-based browsers get the same error?

"Could not display..." is an awfully unhelpful message. Have you met it anywhere else? What does it mean? That is, does it mean MSIE can't find the page, or does it mean it physically can't handle the page code?

g1smd




msg:4394668
 8:50 am on Dec 6, 2011 (gmt 0)

Have you completely cleared the browser cache before testing again?

Variable




msg:4394957
 11:53 pm on Dec 6, 2011 (gmt 0)

Ok, I found a suggestion on another site to slightly modify my .htaccess to the following:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*) http://www.example.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This made all the check server header sites start showing a proper 301 redirect. This appeared to do little on the browsers until I followed the following advice:

Have you completely cleared the browser cache before testing again?


This made it so chrome started working.

I also tried safari on a mac and that worked. So now the problem is only IE9 and it is only on my one pc. I tried it from several other computers and it is working fine.

I am pretty sure it's not a DNS issue as all the example.com/any-valid-page-here/ urls properly 301 redirect to www.example.com/any-valid-page-here/.

So...I've come to the conclusion that my original .htaccess had some minor issue with it that I am not proficient enough to figure out. I've also come to the conclusion that my install of IE9 is perhaps corrupted in some way or seriously confused as a result accessing my flawed .htaccess. So I'm going to re-install IE9 and see if that fixes my problem.

For an additional tidbit of info. After I access example.com and get the extremely useful "Internet Explorer cannot display the webpage" error. I hit refresh and it seems to just sit there and spin with the title of the page "ieframe.dll".

Anyways, I'll re-install IE9 and report back.

g1smd




msg:4394993
 1:27 am on Dec 7, 2011 (gmt 0)

We already determined that RewriteCond %{HTTP_HOST} ^example\.com is not optimum, so don't use that.

RewriteBase / is the default and can be omitted.

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