Forum Moderators: phranque

Message Too Old, No Replies

Cutenews to Wordpress mod rewrite redirect

         

Tasha25

10:07 pm on Apr 12, 2009 (gmt 0)

10+ Year Member



Hi,

I want to start using Wordpress, and I want to be able to redirect Cutenews news post URLs. I was trying to use the following code that I found, but it doesn't work:

# redirect the old cutenews articles
RewriteCond %{QUERY_STRING} (.*)(subaction¦start_from)(.*)id=([0-9]+)(.*)
RewriteRule ^.* http://www.example.com/cutenewsredirect.php?article=%4 [L]

This sends the URL to a php script that then redirects to the new post on Wordpress, although at the moment I've just got it pointing at the index of my site to test it.

The URL I'm entering in the php script is:

http://www.example.co.uk/index.php?subaction=showcomments&id=1224185187&archive=&start_from=&ucat=&

however, unless you strip it down to this, it doesn't work:

http://www.example.co.uk/index.php?id=1224185187

How can I get it so it just uses the id out of the first URL for the redirect, no matter what else is around it?

I'm not particularly a coder or anything so please explain clearly. :)

Thanks!

[edited by: jdMorgan at 2:20 am (utc) on April 13, 2009]
[edit reason] example.com, example.co.uk [/edit]

jdMorgan

2:19 am on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to rewrite *any* URL that has *any* query string appended to it, as long as that query string contains "id=<somenumber>", then this should do:

# redirect the old cutenews articles
RewriteCond %{QUERY_STRING} &?id=([0-9]+)&?
RewriteRule ^.*$ http://www.example.co.uk/cutenewsredirect.php?article=%1 [L]

See the concise regular expressions tutorial cited in our Apache Forum Charter for information on what the regex patterns here mean. This is a very worthwhile investment of your time, as regular expressions are used in mod_rewrite, PHP, PERL, and many other scripting and high-level programming languages.

Jim

[edited by: jdMorgan at 2:21 am (utc) on April 13, 2009]

Tasha25

11:22 am on Apr 13, 2009 (gmt 0)

10+ Year Member



Thank you for your help, but this doesn't work either!

I did some messing around with the URL to see what could be stopping it from redirecting, and it redirects fine so long as the = between "subaction=showcomments" isn't there...

How do I add to the beginning of the regex that I want it to ignore this = so that it redirects with it there?

Thank you!

jdMorgan

8:02 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code posted above does not care about anything except "id=<numbers>" which may be preceded or followed by ampersands and *anything* else. The way th pattern is written, if anything precedes "id=<numbers>" in the query string, then it must end with an ampersand, and if anything follows "id=<numbers>", then it must start with an ampersand. Among other things, this prevents the rule from matching similar query string names, such as "sid=" and prevents matching non-numeric values, such as "id=24B". The optional ampersands in the pattern are effectively "soft anchors" for the desired substring.

Be sure to completely flush your browser cache before testing any new or modified server-side code (e.g mod_rewrite, PERL, or PHP code).

If this does not help, then you have another rule or directive which is interfering with your rule. Find it and correct it.

Jim

Tasha25

8:24 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



Completely cleared my cache... still doesn't work!

I have nothing else in the .htaccess file... I wondered if it was something in the cutenewsredirect.php file that could be causing the problem, but I don't see why:

<?php
{
case "1224185187":
Header("HTTP/1.1 301 Moved Permanently");
Header("Location: http://www.example.co.uk");
break;
}

?>

I'm not sure what else to look for in any file!

Thanks. :)
Tasha

g1smd

8:34 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I am confused. The original Rewrite code at the top of this post is NOT a Rewrite. With a domain name in the target URL, it is actually a 302 Redirect.

Now you reveal the file contains a 301 redirect to the root of the site. So, requests are returned a 302 redirect to a script and the script sends a 301 redirect to the root. That is a problem.

That's obviously not what you want, and I am not quite sure what you actually want - in terms of both URLs used out on the web, and internal names of files inside the server, however it is likely that the first thing is that the 302 redirect should be a rewrite.

The script will then need to capture the value in the parameter and use it to populate the target URL. Testing by redirecting to the root is a good start here though.

Tasha25

8:43 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



I'm sorry... obviously I've gotten a little confused myself (I'm really not used to this and I was working with what I found!).

When I searched for some help on redirecting Cutenews URLs I got the two things that I have been working with... the RewriteCond... etc. in the .htaccess and the cutenewsredirect.php code, but obviously they don't work entirely.

What I want is to be able to redirect URLs. I want to start using Wordpress instead of Cutenews on my site, and there's several posts I'd like to re-post on Wordpress, and have the original Cutenews URLs redirect to the new post on Wordpress when someone tries to access them:

eg:
http://www.example.co.uk/index.php?subaction=showcomments&id=1224185187&archive=&start_from=&ucat=&

will redirect to:

http://www.example.co.uk/newpost/

I hope this explains it? And of course I'd love it if any help could be explained so I can understand further. :)
Thank you,
Tasha

[edited by: Tasha25 at 8:44 pm (utc) on April 13, 2009]

g1smd

9:02 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Is the new URL always exactly
http://www.example.co.uk/newpost/
or does it vary?

Tasha25

9:14 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



I haven't installed Wordpress yet and haven't used it in a while, but I checked a couple of sites and it seems to pretty much be:

http://www.example.co.uk/?p=a post id number here which varies

(e.g.: http://www.example.co.uk/?p=140). :)

[edited by: Tasha25 at 9:19 pm (utc) on April 13, 2009]

g1smd

9:31 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Where are you going to store the list of old numbers and new numbers? I assume that will be a small table in your database.

Tasha25

9:36 pm on Apr 13, 2009 (gmt 0)

10+ Year Member



I've actually solved the problem (with some help from a friend!)

All I needed to do was add:

RewriteEngine On

into the .htacess code:

# redirect the old cutenews articles
RewriteEngine On
RewriteCond %{QUERY_STRING} &?id=([0-9]+)&?
RewriteRule ^.*$ http://www.example.co.uk/cutenewsredirect.php?article=%1 [L]

Thank you for all the help anyway! :) And I'll certainly be taking a look at the Regex tutorial as suggested.

Tasha

g1smd

9:50 pm on Apr 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



No. You still have a double redirect in the system. The job is not finished.

Although what you get out appears to right, the route the visitor takes is not optimum. This will come back to bite you.

jdMorgan

1:11 am on Apr 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteEngine On
#
# [b]Internally rewrite[/b] old cutenews articles requests to redirection script
RewriteCond %{QUERY_STRING} &?id=([0-9]+)&?
RewriteRule ^.*$ [b]/cu[/b]tenewsredirect.php?article=%1 [L]

Jim

[edited by: jdMorgan at 2:22 am (utc) on April 14, 2009]