Lovely module, sorely needed too! However, adding one redirect at a time is VERY time consuming. I have something like 75 to do, some way to do multiple would be a great boon, even if it is just a page with 10 lines of fields to submit at once.

Thanks

Tom

Comments

jjeff’s picture

How do you suppose an interface for this would work? I'm open to the idea, but there are several things that need to be entered for each redirect, so it's not quite as simple as a textarea with 1 URL per line.

tdellaringa’s picture

glad you asked! I'm a usability engineer by trade so I appreciate the concern. My initial thought would be that you would have say 10 rows on the page each with the 5 fields, where the redirect type would change do a dropdown. So you'd have:

alias-to-redirect | to | ? | # | type

Now, I don't know how you process this, but I'm assuming you could run each row through the process the same way you are doing 1 - you just do up to 10.

Granted horizontal space is an issue, you'd get less room for the fields.

However, possibly a better idea is to allow some kind of CSV file to be submitted and run the process on that. I'm just thinking out loud here, I'm not sure what the best way would be. If I could submit a text file that is like

originalURL/bad, newURL/good, ?foo, #link, 301
originalURL/bad, newURL/good, ?foo, #link, 301
originalURL/bad, newURL/good, ?foo, #link, 301
originalURL/bad, newURL/good, ?foo, #link, 301

Then have it processed, that would be the easiest for the user, I think. I just work offline and create my text file, upload the file and you do your magic on it, running each row and adding the redirects.

Again, I have no idea how much work that is on your end, or if it is even possible. Where do the redirects end up - how does it work?

jjeff’s picture

A usability engineer you say!!?? Well there are a great many things that we could use your help on!!

I like the comma separated stuff because, as you say, you can create these as a separate text file and bring 'em in all at once. And this format could also work for export, so we're basically building an import/export system for this module. And the import should be really easy to parse because commas aren't valid URL characters (I think). So I can simply do a split by line break, and then by commas, and run those into the path_redirect_save() function. (I make it sound so easy!)

I'll take a look at this and see what I can come up with.

(Seems like there should perhaps be a similar thing for path aliases as well.)

tdellaringa’s picture

Exactly my thoughts! Glad it makes sense. I think it's probably easier that way than trying to come up with a form that works with all the fields. I'll keep an eye on this thread looking for an update. If you want to e-mail me when it's ready, its pixelmech at gee mail dot com.

Thanks for your hard work!

HorsePunchKid’s picture

I'm afraid commas are valid in URLs, or at least you're likely to encounter them. Some of the crustier CMSes that adopted the "users don't care about URLs" philosophy use them curiously often. Witness:

http://web.worldbank.org/WBSITE/EXTERNAL/0,,contentMDK:20130468~pagePK:5...

*ack* Now that said, I'm pretty sure that spaces are not valid in URLs, nor should they appear in any of the other parts of the input. Or perhaps tabs would work, since tab-delimited files are fairly well supported. CSV could still work, but unless there's a PHP parser library you can count on being installed, it's probably not worth it.

HorsePunchKid’s picture

Title: Need multiple add feature » Import redirects from a delimited file
Priority: Critical » Normal

More accurate title given where this discussion went. A "quick add" feature might be nice, still...

HorsePunchKid’s picture

Here is a proposal:

  • An "import" tab on the admin page.
  • A form therein comprising a file upload widget and a redirect type selector (default: 301).
  • The uploaded file shall be tab-delimited; records separated by /[\n\r]+/.
  • The first two columns list the "path" (from) and "redirect" (to) in that order.
  • An optional third column contains a redirect type to override the default selected.
  • The query string and fragment are automatically extracted.
  • Errors (e.g. from already-existing redirects) will be non-fatal and reported when the import is complete.

Any thoughts? Would it be better to prepare a query and bypass path_redirect_save()?

gemini’s picture

I would be interested in this module to redirect over 4K URLs. I used it to redirect less than 100 URLs and it worked beautifully, but is it gonna handle over 4K just as good?

HorsePunchKid’s picture

I don't think the query for looking up the redirect will be noticeably slower for 4,000 entries compared to 100 entries. Paging through 4,000 entries in the admin interface might be rather burdensome. If necessary, we could perhaps add a quick filter box on the admin page.

gemini’s picture

What I actually did - created a CCK old index reference field and imported all page indexes from the old site. Then I uses custom_errors module and added some code to extract ids from old ulrs and look up in the current db. So far it works fine.

geodaniel’s picture

Subscribing to this.. it'd be great to see.

Dave Reid’s picture

Title: Import redirects from a delimited file » Add importing and export for CSV and XML
Version: 5.x-1.x-dev » 6.x-1.x-dev
Assigned: Unassigned » Dave Reid

Basic export functionality has just been committed to the 6.x-1.x-dev version. Sorry, new features are not going to be ported back to Drupal 5, so upgrade! I'm also working on getting the import working.

silurius’s picture

Dave, are there plans to include import functionality as well? Currently trying to decide how I want to handle replicating the ~100 redirects I created in a dev site today to a QA site.

Dave Reid’s picture

There are, but I'm looking into how to best implement it. Probably what I'm going to go with is to implement integration with the Node import module so that I don't have to recreate and maintain import functionality.

jeffschuler’s picture

Subscribing!

The ability to bulk-import would be useful on it's own, and would also allow for some other cool functionality, (within Path Redirect or in other/new modules.)

For instance: Fairly frequently, I wish I could auto-generate appropriate redirects when an automated alias path is changed.
ex: Say I want to change all of my Plant content type nodes from the default content/[title-raw] to plant/[title-raw]. A glue module might auto-create redirects for all old=>new paths...

epersonae2’s picture

I have over 100 static pages to redirect for a current project. I have a spreadsheet listing the old pages and the new paths/node IDs. In lieu of built-in module functionality, do you have any suggestions for importing the redirects directly into the database? (Otherwise, I'm probably going to see if I can do the .htaccess using a CSV as the starting point.)

marvil07’s picture

subscribing

Dave Reid’s picture

Ugh, I still can't decide how I want to implement this. I want to use a solution that is general and not coded by myself just for this. I'd rather use something that has some traction in the community. I'm leaning more towards the Import/Export API. Ugh.

mediamash’s picture

hi guys, what is the current status for importing redirects? thx

dakku’s picture

++ subscribing

silurius’s picture

Just checking in. How's it going with imports? Like epersonae2 says, I am getting ready to move and reorganize a few web sites and am still watching this development with interest.

dakku’s picture

I had to do this recently and tbh if you have direct mySQL access, it is easy enough to import a csv or excel file. I used navicat, but any mySQL front end will do. I agree it would be a handy feature to have, but it is not a deal breaker for me anymore. However, it is quite important for someone without direct access to mySQL or if you are building a website to be used by your client/non techy front end users.

silurius’s picture

Good suggestion, dakku. Can't say why I didn't think of trying DB exporting and importing myself.

I assume it's just a couple of columns in my case since all of my redirects are 301s.

liquidcms’s picture

yup XLS or similar import would be very helpful here.. subscribing

pixelsweatshop’s picture

subscribing

redben’s picture

subscribing

scottatdrake’s picture

Subscribing as well.

conorc’s picture

Hi Janak,

I'm interested in your idea for people who do have direct access to MySQL. With DBScripts this might be handy ;-)

This is a line from the path_redirect.sql file:
INSERT INTO `path_redirect` VALUES (2,'index.html','<front>','','','',301,1280098921);

It seems fine but I'm not sure about the value immediately after the 301. Can this be ignored if I was to leave it blank for up to 1,400 new entries do you know?

Thanks,

conorc’s picture

ah, I've just realised it's only the time-stamp in epoch format.
1280098921 = Sun, 25 Jul 2010 23:02:01 GMT

happy days.

Dave Reid’s picture

Status: Active » Postponed

Still don't have a definitive good import/export API as per #18.

marvil07’s picture

It is possible to use tools like phpmyadmin to generate CSV, but you can also import them, so if you do that, you can use table wizard module + migrate (which first version was based on node_import ;-)). So, I hope this patch would be relevant here #801340-1: path_redirect module support

gthing’s picture

I was able to do a mass import from a CSV file. My CSV file contains the requested URL and the node id that the requested URL needs to redirect to. It looks like this:

business/reduce-the-anxiety,155
press-releases/most-popular-blog-posts-of-2010,335
part-time/top-11-2011-trends,337
business/responsibility-accountability-and-team-work,225
part-time/ten-accounting-finance-secrets-for-start-ups,274

The other things you need in order to add a path redirect are assumed in my script to be constant, but you could easily add another field to the CSV for redirect type, etc. You can actually insert new path redirects with the following SQL command:

INSERT INTO `path_redirect` VALUES ('','requested/path','node/123','','','',301,'')

The first and last values are left blank because they will be automatically generated. The 'rid' field is generated when you run the command, and the timestamp is generated the first time the URL is used. Note that the requested URL should not have any trailing or beginning slashes or it won't work. Also, you have to redirect to the node ID, redirecting to the path alias did not work.

I named my .CSV "byid.csv" (because it is inserting redirects by the node ID) and put it in the same directory as this script:

<?php

/* Function to write to the DB */

function replace($orig,$new) {
	$string = "INSERT INTO `path_redirect` VALUES ('','" . $orig ."','node/".$new."','','','',301,'')";
	echo $string . "<br />";
	//mysql_query($string); /* Uncomment when you're ready to execute! */
}

/* CONNECT TO THE DATABASE */

	$dbhost = 'localhost';
	$dbuser = 'username';
	$dbpass = 'password';

	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('mysql connect error');

	$dbname = 'databasename';
	mysql_select_db($dbname);

/* Loop through the CSV */

$row = 1;
if (($handle = fopen("byid.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $num = count($data);
        echo "Executing Command: ";
        $row++;
       
        replace($data[0],$data[1]);

    }
    fclose($handle);
}

mysql_close($conn);

?>

I'm sure this is a bit janky and it does not really do any sort of error correction, so feel free to clean it up a bit or use the code to create a separate module (I don't really know how to do that). I commented out line 7 which actually writes to the database, so you can run this script and see all the queries that will be run and test a few of them manually. Once everything looks good just uncomment line 7 and run the script. You should be good to go!

Edit: Do you guys (maintainers) want to integrate this into the core module? If not I wouldn't mind using this as an opportunity to learn how to code my first official module.

gabrielu’s picture

Assigned: Dave Reid » Unassigned
Status: Postponed » Needs review

I've created a module for this, see http://drupal.org/project/path_redirect_import

VaporCreations’s picture

gabrielu, Props go your way dude. Good show! Just trying it out now.