Welcome to WebmasterWorld Guest from 3.233.226.151

Forum Moderators: Ocean10000 & phranque

Message Too Old, No Replies

rewrite to www, but keep url string

help!

     
2:32 am on Oct 18, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


Hi,
Seems this should be simple but I'm ripping my hair out here and would love some help by you knowledgeable folk!

As far as htaccess goes I'm a cut-and-paste coder so bare with me..

I'm using this code to divert reqeusts for example.com to www.example.com


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


However, when I try example.com/about.php I get sent to www.example.com, without the rest of the url. Am I missing something?
2:48 am on Oct 18, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


Ok, new question..

I removed the htaccess altogether from the server and I see it's still redirecting from non-ww to www, without my file. Is there something else I could look for on the server that could be causing this? Or is this something I should ask the host about?
5:11 am on Oct 18, 2011 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15928
votes: 884


If you are on shared hosting there is probably an option for with/without www redirecting. If you can't find it in the fine print, grit your teeth and ask. But redirecting from a full address on the non-canonical domain to the index file of the canonical domain smacks of, er, serious incompetence. I mean, you might do it on purpose but nobody would do it generically.

Have you established that you can have an htaccess? Some hosts won't even let you.
6:23 am on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


The initial canonicalisation code should have worked, but does not redirect non-canonical requests such as www.example.com:80/ with a port number.

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


Don't forget to escape literal periods on patterns.

Do non-www requests resolve to a different folder inside your server? If they do, the .htaccess in that folder handles those requests.
6:47 am on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


By the way, the thread title is misleading. This code is a redirect to a new URL, not a rewrite.

RewriteRule can be configured to do redirects or rewrites. It is important to do the right one.
7:34 am on Oct 18, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


I found that the code I originally posted just didn't work on my server so I changed it to this, which I found on the host website:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(([a-z0-9_]+\.)?example\.com)$ [NC]
RewriteRule .? [%1%{REQUEST_URI}...] [R=301,L]

I have another htaccess question that I might as well throw in here as well.. I have moved most of my content to a subdomain on my original domain, but with the same structure. I would like requests like example.com/this/page.php to go to sub.example.com/this/page.php ... BUT only if the original url doesn't exist. (because some of them do!)

Any help is really, really appreciated.

Sorry about the misleading title, I really don't know what I'm talking about! :)
7:36 am on Oct 18, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


oh, and yes,.. i did switch to going from www to non-www in the second code.
7:43 am on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


You'll now find that your latest code does not redirect non-canonical requests such as example.com:80/ with a port number.
5:02 pm on Oct 18, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


In what instances would there be incoming urls like the one above, with a port number?

Ignorance is not bliss!
6:46 pm on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


...when a competitor spots the flaw and sets up external links promoting Duplicate Content on your site.
8:38 pm on Oct 18, 2011 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15928
votes: 884


OK, seriously now:

If you are small enough that you are writing all your own code and need to ask htaccess questions as you go, are you really likely to have competitors with that level of know-how?

:: giggling insanely at the idea of finding cutthroat competition in the realm of, say, Inuktitut + Humour + Morphology ::
8:42 pm on Oct 18, 2011 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


You'd be surprised.
5:12 am on Oct 19, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


very very surprised. ;)
5:21 am on Oct 19, 2011 (gmt 0)

Full Member

10+ Year Member

joined:Apr 7, 2006
posts:201
votes: 0


I may be small enough that I write my own code, build/design sites and do all the marketing myself, but I AM big enough to have many competitors with "that level of know-how". Seriously.
6:49 am on Oct 19, 2011 (gmt 0)

Senior Member from US 

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

joined:Apr 9, 2011
posts:15928
votes: 884


Ouch. Sounds like you've got some catching-up to do, then. Mwa ha ha.