Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-07-19 17:27:54

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Best, simple 301 for people visiting http://here to http://www.here?

Hi folks,

I’ve wimped out a bit here I’m afarid — I usually try lots of solutions before opting to post here to ask… This time I have tried a couple of things but failed and guessed I was likely to get a less than an ideal answer on my own as this is a common topic with lots of answers it seems.

Please can anyone with more knowledge of this than me (all of you? ;) who’s done this before suggest the best way to have people visiting a site via http://example.com/cheeses be neatly redirected to http://www.example.com/cheeses?

Right now they end at my 404 page.

So far all I have is the domain set in TXP prefs to www.example.com and in .htaccess I added:

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

Thanks lots in advance for and comments.

Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#2 2010-07-19 18:04:46

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,477

Re: Best, simple 301 for people visiting http://here to http://www.here?

<IfModule mod_rewrite.c>

	RewriteEngine On
	RewriteBase /

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

	// more

</IfModule>

Edit: Added the missing $ in the RewriteCond.

Last edited by GugUser (2010-07-19 19:06:43)

Offline

#3 2010-07-19 18:41:47

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: Best, simple 301 for people visiting http://here to http://www.here?

Thanks GugUser — but with that .htaccess, if I visit

http://www.examle.com/a-section/128/cheese

I get re-directed to my 401 page at this url

http://www.example.com/index.php/128/cheese

— the section is being replaced by index.php.

I don’t know if the easiest solution will be a further tweak to the .htaccess or if there is a preferred/best plugin — it would be great if it was just a tweak to .htaccess (just sorry I don’t know how and am boring you and others with this).

Cheers, -Alan

Last edited by alanfluff (2010-07-19 18:42:01)


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#4 2010-07-19 19:05:04

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,477

Re: Best, simple 301 for people visiting http://here to http://www.here?

In my previous example, in the RewriteCond the $ was missing.

Your complete TxP-.htaccess-File must contain:

<IfModule mod_rewrite.c>

	RewriteEngine On
	RewriteBase /

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


	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -d
	RewriteRule ^(.+) - [PT,L]

	RewriteCond %{REQUEST_URI} !=/favicon.ico
	RewriteRule ^(.*) index.php

	RewriteCond %{HTTP:Authorization}  !^$
	RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]

</IfModule>

Offline

#5 2010-07-19 19:15:41

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: Best, simple 301 for people visiting http://here to http://www.here?

Thanks — I’ve added your code, but the same is happening — perhaps it’s what is already in my .htaccess file? This is it in total now:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
.
RewriteRule ^(.*) index.php
AddHandler php5-script .php
.
RewriteEngine On
RewriteBase /
.
RewriteCond %{http_host} !^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [r=301,L]
.
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
.
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) index.php
.
RewriteCond %{HTTP:Authorization}  !^$
RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]

The lines with only periods in — the periods are only here for readability, not in the actual file.

I am using RewriteEngine On twice, I probably shouldn’t, but assuming that’s not the problem..? And I’ve removed your healthy test, I could add it but I am confident the environment supports re-write.

Thank you for persisting with my sad case… If I have been stupid in removing your test etc I’ll add it back in an instant. Thanks again for any further comments on this.

Cheers, -Alan

Last edited by alanfluff (2010-07-19 19:16:46)


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#6 2010-07-19 20:57:37

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: Best, simple 301 for people visiting http://here to http://www.here?

Ok, a bit freaked out now.

After getting the problems, I setup a duplicate site so I could test w/o hurting my live site.

I added aks_header (a bit of an overkill just to cure my error, but I was just testing).

The error persisted.

So I went back to basics and removed the original .htaccess code intended to do a 301 for any page not with a www prefix to it’s counterpart with a www prefix, I removed the plugin I’d just added, I removed all .htaccess code I’d added (but much tks to GugUser for suggestions all the same) and tried the site with just this .htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [PT,L]
RewriteRule ^(.*) index.php
AddHandler php5-script .php

And it works perfectly. If I visit the www… version of any page or the non-www prefixed version of the same page, I get no error and the URL is just respected.

I started this all as Google suggested I might want to add a 301 to move all arriving non www urls to their www counterparts (as with www is the preference for this client/site).

It seems I still have to achieve that redirect, but that failing aside, all is fine.

So:

  1. Sorry if I’ve wasted anyone’s time reading this and thanks TXP (again) for working out of the box
  2. Thanks GugUser for your help
  3. And lastly, does anyone know how to impose a 301 for all URLs so www is consistently used?

Thanks lots in advance and even if no one knows the answer, I hold out the faint hope this thread might help someone as weak-minded as I ;)

Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#7 2010-07-19 21:24:30

GugUser
Member
From: Quito (Ecuador)
Registered: 2007-12-16
Posts: 1,477

Re: Best, simple 301 for people visiting http://here to http://www.here?

With this it must work. Maybe you have to adjust the path in RewriteBase / and without duplication of code.

All my TxP-Websites runs with that rewrite.

Why did you write AddHandler php5-script .php in the .htaccess-file?

Offline

#8 2010-07-19 21:29:17

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: Best, simple 301 for people visiting http://here to http://www.here?

I’ll try that (remove dupe code, check RewriteBase) and report back here.

Not being very au fait with .htaccess I wasn’t sure if AddHandler php5-script .php was ‘normal’ but didn’t remove it as I assumed it came from the default (I should have checked). With hindsight, I think it came from an install script of some sort (my guess) when I setup the hosting (at(mt)). I assume it says “use PHP5” but I was a little surprised to see it there. May try removing…

Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#9 2010-07-19 21:50:59

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: Best, simple 301 for people visiting http://here to http://www.here?

Shouldn’t you just remove the first www?

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

Offline

#10 2010-07-20 05:45:21

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,254
Website GitHub Mastodon Twitter

Re: Best, simple 301 for people visiting http://here to http://www.here?

Did you try:

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

	#RewriteBase /relative/web/path/

	RewriteCond %{REQUEST_FILENAME} -f [OR]
	RewriteCond %{REQUEST_FILENAME} -d
	RewriteRule ^(.+) - [PT,L]

	RewriteCond %{REQUEST_URI} !=/favicon.ico
	RewriteRule ^(.*) index.php

	RewriteCond %{HTTP:Authorization}  !^$
	RewriteRule .* - [E=REMOTE_USER:%{HTTP:Authorization}]
</IfModule>

Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#11 2010-07-20 11:25:31

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: Best, simple 301 for people visiting http://here to http://www.here?

Thanks Els, Yiannis, GugUser — it works, what I used was what GugUser posted — the mechanism was failing, and that threw me off, but I think it was failing because I had original TXP standard .htaccess code in there, with the tested code from GugUser — now with only the code linked to above, I can visit non www versions of URLs and the page loads fine and the URL is deftly flipped to it’s www alter ego.

THANK YOU! Grateful to you all.

Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

Board footer

Powered by FluxBB