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

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

Apache Web Server Forum

    
Permanent Redirect
ZakAltF4



 
Msg#: 4412714 posted 4:47 pm on Jan 31, 2012 (gmt 0)

Hello all,

I am in the process of streamlining our website and am concerned about the fact that we are using BOTH www and non-www. I am going to use a 301 redirect, but I am concerned about losing functionality of the site due to numerous plugins etc that are hard coded to use WWW. So, I suppose my question is, if I use this code that I edited from several sources:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www..*
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^([^.]*).(com|com/)
RewriteRule ^.*$ www.%1.%2%{REQUEST_URI} [R=301,L]


Will this solve our duplication issues sitewide? For instance. if someone visited:

http://example.com/dir1/dir2/dir3/product.php

Would they effectively be led to

http://www.example.com/dir1/dir2/dir3/product.php ?

Basically I am wanting to know if this will work for sitewide redirects versus just redirecting the landing page.

Thanks!

Zak

 

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4412714 posted 9:46 pm on Jan 31, 2012 (gmt 0)

Urk. Before anything else, get your dots . sorted out. In Regular Expressions, . means "any one character". If you mean a literal period, like the one in "dot com", you have to say \.

RewriteRules apply to all requests, not just the ones that your user typed in from "outside". So that includes any and all include files and similar.

All your Conditions can be merged into one.

:: fingers crossed, because last time I did this I garbled the punctuation ::

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

That is: if the host is anything other than exactly www.example.com or exactly nothing, redirect to...

I don't know where the %1 and %2 in your Rule are coming from. The only thing you need to capture and reuse is the request.

The form ^.*$ is always redundant. By default, Regular Expressions start as soon as they can and go on as long as they can. You only need anchors when you are looking for specific text, or capturing text in a specific location.

ZakAltF4



 
Msg#: 4412714 posted 9:53 pm on Jan 31, 2012 (gmt 0)

Thank you for your explanation! This is probably the best explanation I've found on the web ... My understanding to the single "." was wrong! I have used your example, and the redirect is working flawlessly .. I have tested the Headers and they are behaving exactly how I was hoping. Thanks Again!

-- Zak

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4412714 posted 10:06 pm on Jan 31, 2012 (gmt 0)

because last time I did this I garbled the punctuation ::
RewriteCond %{HTTP_HOST} !(^www\.example\.com$)?

That's garbled, but in a way that it would appear to work for some requests.

Use:
!^(www\.example\.com)?$
penders

WebmasterWorld Senior Member penders us a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



 
Msg#: 4412714 posted 10:32 pm on Jan 31, 2012 (gmt 0)

That is: if the host is anything other than exactly www.example.com or exactly nothing


I noticed this in the OP, but why do you need to check that the HTTP_HOST is not empty?

ZakAltF4



 
Msg#: 4412714 posted 11:04 pm on Jan 31, 2012 (gmt 0)

g1smd,

I did change that ... Because it appeared to work, could you explain the difference between (^...$)? and ^(...)?$ ?

lucy24

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4412714 posted 2:45 am on Feb 1, 2012 (gmt 0)

why do you need to check that the HTTP_HOST is not empty?

You should have nipped in before g1 got here, because I'm quoting him. It's something about HTTP 1.0 not sending a hostname, so the {HTTP_HOST} might come through as completely empty.

OK, let's see how many different ways I can mangle the punctuation before someone's server melts down and I am banned from the Forum.

Right version:
!^(www\.example\.com)?$
Wrong version:
!(^www\.example\.com$)?

The leading ! is pretty hard to mangle; it's the part that means "is not".

^{blahblah}$ means "the whole text, from beginning to end"
? means "the immediately preceding character or group (either in brackets or parentheses) may or may not be present"

By putting the anchors inside the parentheses, the Wrong Version permits your HTTP_HOST to be... well, just about anything really. Or, more to the point, it permits it to not be just-about-anything.

When testing, don't just feed in things that are supposed to work. Also feed in things that are supposed to not work, including some perfect garbage.

And then wait a few minutes and check your error logs to make sure they're not suddenly ballooning out of all proportion and/or carrying the same timestamp as your regular logs. Been there. Done that.

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