Am developing new version of a website in Drupal (current is static HTML). Installed globalredirect as I wanted to set up 2 aliases for some pages: the primary alias that the page is "known by" on the new site (which has the same domain as the old) and an alternative alias used to maintain SE pagerank as far as poss:
e.g. have
www.example.com/new_page - URL/alias of a page on new site (based on previous_page.html on old site)
www.example.com/previous_page.html - URL of related page on old/current website
I want to set up Drupal so that when I switch the sites over the second of these to redirects to the first.
However if there are 2 aliases to a page/node, globalredirect is happy with whichever one has been requested by the browser. This is because drupal_get_path_alias() caches its results for the lifetime of the page request.
By changing lines 35 - 36 from
// Check the path (eg, node/123) for an alias. If one is found, redirect.
if ($alias = drupal_get_path_alias($request)) {
to
// Check the path (eg, node/123) for an alias. If one is found, redirect.
// We want the "primary" alias for the relevant system path, and so
// have to do work directly with the database because Drupal's
// get_path_alias() function caches its results during
// the lifetime of the page request
if ($alias = db_result(db_query("SELECT dst FROM {url_alias} WHERE src = '%s'", drupal_get_normal_path($request)))) {
then I get the behaviour I want.
Maybe others could use this also? I've attached my modified version of the module, with this simple change, in case it's of use to anyone.
(By the way, when creating a 2nd alias for a given page, there seems to be no way of knowing which will become the "primary" alias for the system path concerned (i.e. the one that would be returned by drupal_get_path_alias(system_path)) and hence feature in menus or in the listing of content at admin/content/node - it's a matter of trial and error to find out whether the new alias or the old one is "primary". However, from my experience to date it would appear that the "primary" alias for a system path does not change ... although it might if another alias is set up for the system path in question.)
| Comment | File | Size | Author |
|---|---|---|---|
| globalredirect.module.1.tar_.gz | 1008 bytes | gpk |
Comments
Comment #1
nicholasthompsonThis is very thin ice you're treading on - you're not even meant to be able to map 2 aliases to 1 system path and in any case, this is CREATING duplicate content, not solving it.
However, after re-reading through your post I see that you want your NEW URL to be primary but to redirect from the old one. For your needs, I would recommend creating you new URL and mapping it to the system path as normal, but then using JJef's snazzy Path Redirect module to redirect all old URL's to the new URL's.
Bear in mind that I dont think you'll actually "preserve" any page rank by redirecting old URL traffic to new pages. You will, however, stop the SE's seeing a shed load of 404's...
I dont know what the spec for your project is - but I'd be inclined to at least consider preserving ALL previous URL's and then just use your new URL structure for new content. This way you'll full preserver Page Rank.
But to come back to the point of this issue - I appreciate the suggested change to the module, however the point of Global Redirect is to be a lightweight module which simply redirects from source URL's to aliased URL's if they exist. The concept of primary/secondary/tertiary aliases has been discussed before and, although possible, was decided to be outside the scope of this module as its quite a minority requirement. Its also a requirement that I believe is better suited to Path Redirect which has a nifty interface for managing your secondary/old aliases for redirection.
Comment #2
gpk commentedThanks for the prompt reply, Nicholas.
>you're not even meant to be able to map 2 aliases to 1 system path
Well it's been possible since 4.7...!
Thanks for the pointer to the path redirect module. Somehow I'd missed that one though I'm sure it was on my list. Always hard to find the exact module one is looking for!
Maybe this post will now, finally, and as I had originally hoped, actually help someone else too! (Bah, I was so pleased with my first contributed line of code!!!!)
Have a great weekend + thanks again,
gpk
Comment #3
nicholasthompsonNo problems - hope path_redirect works for you! Nice try with the fix too...
Comment #4
davehensley commentedI ran into this same problem when I helped convert (WebProNews) over to Drupal a few months ago. Prior to that, all of the articles were in static .html files, which had been generated from data contained in a mysql database. Basically, I wanted to change all the old article URLs to Drupal-style URLs (of which there were over 30,000), but since all of our incoming links were currently pointing at the old-fashioned .html URLs, they had to be redirected to the new URLs to avoid duplicate content issues with the search engines.
To solve this, I first used the Pathauto module to create all the new aliases automatically. Like you, I made a change to globalredirect to fetch the new URL straight out of the url_alias table, although I added "ORDER BY pid LIMIT 1" to the end of my sql query. Since the pid column auto-increments, and all the new-style URLs were already created by Pathauto, I then wrote a perl script to insert all the old-style URLs into the table (with higher pid values), so that my modified globalredirect will automatically redirect all old-style URLs to the new-style URLs every time.
Dave.