Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-08-12 21:57:26

NeilA
Member
From: Blue Mountains, Australia
Registered: 2004-08-15
Posts: 316
Website

htaccess and redirecting changed URLS

I’m overhauling a blog of mine, and in the process I would like to be able to change the permalink scheme

For example, under the existing scheme I have lots of entries thus: http://mydomain.com/blog/articlenumber/articlename.

In the new version, I would like to have http://mydomain.com/article/articlename. – so change the section, and drop the article number from the permalink scheme.

I know how to make those changes within the TxP setup and have done so in a dev version of the site, but I wonder if it’s possible to have the old URL scheme automatically redirect to the new one using htacess?

I suspect I will have to keep the article number, but is it possible to use htacces to change the ‘blog’ part of the URL to ‘article’? If so, how?

Thanks!

Last edited by NeilA (2007-08-12 23:49:03)


Neil – Blue Mountains, Australia

http://westserve.org
http://ministrygrounds.net.au

Offline

#2 2007-08-13 06:12:13

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

Re: htaccess and redirecting changed URLS

Neil,

try to add this line to your .htaccess file, just above Textpattern’s own RewriteRules starting with RewriteCond %{REQUEST_FILENAME} -f [OR]

RewriteRule ^blog/[0-9]*/(.*) http://mydomain.com/article/$1  [R=301,L]

This would inform any user agent, browser, or crawler that your previous URLs have permanently moved to their new location.

Offline

#3 2007-08-13 07:26:46

NeilA
Member
From: Blue Mountains, Australia
Registered: 2004-08-15
Posts: 316
Website

Re: htaccess and redirecting changed URLS

Robert,

You’re a legend! I won’t pretend I understand that line, but it works a treat. Thanks very much.

Cheers


Neil – Blue Mountains, Australia

http://westserve.org
http://ministrygrounds.net.au

Offline

#4 2007-08-13 08:02:59

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,330
Website Mastodon

Re: htaccess and redirecting changed URLS

Well, as they say: mod_rewrite is Apache’s Swiss army knife – it helps a lot, and it potentially hurts a lot (especially inside one’s head).

As a little teaser on how to understand this line, I’ll try to break it into its various parts:

  • RewriteRule: This keyword introduces the rule
  • ^blog/: Match any incoming URL which starts with “blog/”
  • [0-9]*/: Match characters from the set of 0 to 9 (i.e. all digits), even repeatedly as designated by the asterisk, plus a subsequent slash
  • (.*): Match any character (.), repeated at will (*), and keep that matched text in an internal memory (that’s indicated by the paratheses). This is the very last part of the URL, the article’s slug.

So, this is the condition the incoming URL has to fulfill to get stuffed into the rewrite magic-o-mat. And what shall the outcome of this be?

  • http://mydomain.com/article/: Take this literally.
  • $1: Retrieve the contents of the first store we put there during the condition processing above, i.e. the article slug.
  • [R=301,L]: Redirect to the newly computed URl by sending it to the visitor’s browser with a HTTP response code of 301 Moved permanently. Do not process any subsequent rules, this is the L ast.

Now, you could probably imagine what a great pile of satisfaction stems from building a working rewrite rule :))

Offline

#5 2007-08-13 10:02:35

NeilA
Member
From: Blue Mountains, Australia
Registered: 2004-08-15
Posts: 316
Website

Re: htaccess and redirecting changed URLS

Hey, I can actually follow the flow/logic of that Robert.
The syntax is new to me, but you explain it well…

Thanks again


Neil – Blue Mountains, Australia

http://westserve.org
http://ministrygrounds.net.au

Offline

Board footer

Powered by FluxBB