Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-11-24 14:28:35

kellner
Member
Registered: 2004-02-27
Posts: 21
Website

... add a custom RewriteRule for links to imported posts

I imported a Nucleus weblog (via WordPress) into Texptattern.

The old Nucleus links looked like this:
http://site/index.php?itemid=1234

The new Txp links, as you know, look like this:
http://site/index.php?id=1234

The Txp site runs with clean url mode, with the out-of-the box .htaccess code of 4.0.2:

IfModule mod_rewrite.c
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) – [PT,L]
RewriteRule ^(.*) index.php
/IfModule

I thought of adding a RewriteRule to redirect from itemid to id (even though Txp is in clean mode, the messy mode links still lead to the desired target), but don’t know enough about the logic behind the rewrite rules that are already there (and work) to figure out where it has to go.

I thought of using this rule:

RewriteRule index.php?itemid=(.*?) index.php?id=$1

If I add it before the existing rule that rewrites everything to index.php, the id will be removed, no?
Adding it afterwards will remove the id as well …
But removing the general index.php rule messes up clean url mode.

Any ideas how I can both redirect the old Nucleus links and maintain Txp’s clean url mode?

Thanks,

Offline

#2 2005-11-24 14:33:26

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: ... add a custom RewriteRule for links to imported posts

Off the top of my head:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule index.php?itemid=(.*?) index.php?id=$1

RewriteRule ^(.+) – [PT,L]

RewriteRule ^(.*) index.php
</IfModule>

Offline

#3 2005-11-24 14:47:18

kellner
Member
Registered: 2004-02-27
Posts: 21
Website

Re: ... add a custom RewriteRule for links to imported posts

That doesn’t work.

As far as I understand the mechanism of rewrite rules: the result of each preceding rule is parsed by the next one. By that rule, if I have rewritten the itemid-URL to http://site/index.php?id=1234, that will be parsed by the final rule that rewrites everything to index.php, thus stripping all the parameters, no?

I don’t understand what the PT and L flags do, though.

Offline

#4 2005-11-24 15:43:15

Sencer
Archived Developer
From: cgn, de
Registered: 2004-03-23
Posts: 1,803
Website

Re: ... add a custom RewriteRule for links to imported posts

These three belong together:
<code>RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) – [PT,L]
</code>

They say, if file exists [OR] directory exists, do nothing and ignore all following rewrite stuff (that’s the [L]).

RewriteRule ^(.*) index.php
This last line says: Rewrite everything to index.php.

If you want to add additional rewrite rules for other distinct cases, you’re usually best of by putting them after RewriteEngine On and before the first RewriteCond

You wrote:
RewriteRule index.php?itemid=(.*?) index.php?id=$1

The pattern you are trying to match has a “?” in it which is a special character in regex meaning the previous element appears 0 or 1 time. If you want to match a literal ? you’ll have to escape it.
I am using permalinks of the style /section/id/title and used the following to rewrite my old incoming URLs.

RewriteCond %{QUERY_STRING} itemid=([0-9]+)
RewriteRule .* /article/%1/? [R=301,L]

Note that I am specifying a permanent redirect [R=301] (which leads to a HTTP response to the client, who then requests the new url) and an [L] to ignore the rest of the rules.

Just to make it complete:
PT means Passthru and is explained in the apache mod_rewrite manual:
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteRule
(“This flag forces the rewriting engine to set the uri field of the internal request_rec structure to the value of the filename field. This flag is just a hack to be able to post-process the output of RewriteRule directives by Alias, ScriptAlias, Redirect, etc. directives from other URI-to-filename translators.[…] You have to use this flag if you want to intermix directives of different modules which contain URL-to-filename translators”).

Offline

#5 2005-11-24 17:21:08

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: ... add a custom RewriteRule for links to imported posts

Damn flags. Thanks Sencer. Does that work for you, kellner?

Offline

#6 2005-11-24 18:01:58

kellner
Member
Registered: 2004-02-27
Posts: 21
Website

Re: ... add a custom RewriteRule for links to imported posts

Blissful! Thank you very much, Sencer. Your example works perfectly for my setup, and the explanations are very helpful.

Offline

Board footer

Powered by FluxBB