3

I am redirecting most of the pages of my old website to a new subdomain of the same domain, from http://www.domain.com/www-page_url to http://new.domain.com/new-page_url/with-different-slug. This new URL value exist in (almost) every page of the old website.

How would I proceed to create a general rule that permits the redirection from the page itself to new.domain.com/page_url existing field value?

3

You should be able to get this to work using the Rules module, using the "Drupal is initializing" event. That will ensure that the rule gets triggered BEFORE the actual content is shown. I'm not sure (actually I really doubt ...) if using Rules event " Content is viewed " (not " Node is viewed " ...) will work (typically that's 'too late'). Refer to "How to specify a Rules event like "Content is 'going to be' viewed"?" for more details on that.

If you can think of something to use a (new) URL like http://new.domain.com/new-page_url?newslug=with-different-slug (instead of http://new.domain.com/new-page_url/with-different-slug), then you can make the value of the URL argument (= the value of the newslug argument in this example) available as new variable to Rules, simply by using the Rules URL Argument module. Here is a quote about it (from its project page):

... provides two rules conditions based on URL arguments:

  • check if a URL argument is present.
  • compare the value of an URL argument.

It also provides an action that makes the value of an URL argument available as new variable to Rules.

The newly provided Rules actions and conditions can be found under the "URL Argument" conditions and actions groups in the rule configuration interface.

For an example of how to use it, refer to Comment # 3 in issue # 1686360 which is about "Pass an amount to the url, ie; site/content/node-title?amount=10 or /node-title?=amount:10". Here is the relevant part of it:

  • add a "Check if URL argument exist" condition to your rule and set the "ARGUMENT" value to "amount".
  • add "Provide URL argument value" action to the rule and set "URL ARGUMENT NAME" value to "amount" and in the "Provided variables" section "ARGUMENT FROM URL - Variable name" to e.g. "amount_from_url".

You will now be able to use the amount value as a variable "amount-from-url" for other actions in this rule.

PS: If you're not familiar (enough/yet) with Rules, checkout the 32 (!!!) great, and free, video tutorials Learn the Rules framework, organized in 7 chapters.

1

Create node.tpl.php file, and at the top of this file, you can retrieve the field value and redirect the user to new url


update

In template.php of your theme folder create function

function theme_preprocess_node(&$variables){
     $node = $variables['node'];
     $wrapper = entity_metadata_wrapper('node',$variables['node']);
     $variables['new_website_url'] =  $wrapper->field_new_website_url->value();
}

Now in node.tpl.php (Copy the source of this file,under View Source, if you don't have it or you didn't theme your nodes) the $new_website_url variable is available.

You can simply use drupal_goto function to redirect to new url.

Hint: Entity module is needed to use entity_metadata_wrapper

Hint: If there are more than a content type and one of them need to be redirected, you can check for $node->type and also set THEME_HOOK_SUGGESTION to redirect the page only for a certain content type.

  • thanks, can you please give me a practical example to get the variable? let's say the field is called "field_new_website_url". what would be the PHP code to insert into the template? thanks – Mau Feb 1 '16 at 22:34
  • 1
    @Mauro I update the answer – M a m a D Feb 2 '16 at 2:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.