2

We migrated a site from custom CMS to Drupal. The content has been migrated successfully, but there is a problem with URL mapping.

In old site the URLs were like domain.com/artists/Artist_ID/Artist_Name (e.g. domain.com/artists/5/picasso), where the URL would work without the last argument part (picasso).

With the Drupal site all URLs use domain.com/artists/Artist_ID as format (e.g. domain.com/artists/5).

They have shared the old site's links on social media where URLs are like domain.com/artists/5/fabien-m%25C3%25A9relle which used to work fine in the old site because last part didn't matter. The same URLs do not work on the Drupal site.

So can I make Drupal ignore the last part of the URL?

2

You can use Apache's mod_rewrite to map all the old URLs shared on social media sites into appropriate new ones, including ignoring the latter part. However, Drupal 7 have a quirk. You need to rewrite to unclean URLs for it to work.

So first, turn off clean URLs (which also means turning off PathAuto) on your site. Then look up one of your artist's pages and figure out what the real URL is. Let's say that for artist 5 (Fabien Mérelle), it is:

domain.com/?q=taxonomy/term/5

Now you should be able to insert the following rewriterule:

RewriteRule ^artists/([0-9]+)/.*$ ?q=taxonomy/term/$1

(You must of course change the target to whatever your test showed the true query string to be.)

The above rule will change artists/5/fabien-m%25C3%25A9relle into ?q=taxonomy/term/5.

Verify that that the rule "works" (i.e. that the URL is really rewritten). Then you should be able to turn clean URLs and PathAuto back on, and it should still work.

The rule consists of two parts. The first (^artists/([0-9]+)/.*$) is the pattern to look for, the second (?q=taxonomy/term/$1) is the target. They're separated by a single space.

The ^ in the pattern indicate the start of the local path, so it looks for a pattern starting with artists/ followed by a number ([0-9+]) followed by a slash (/) followed by anything (.*). The final $ in the pattern is just a marker for the end of the local path. The parentheses around the number stuffs the number into the variable $1. The target simply consists of the static string ?q=taxonomy/term/, followed by the value of the $1 variable.

Note: You must place the line with the rewriterule inside the .htaccess file for the site, inside the block that starts with:

# Various rewrite rules.
<IfModule mod_rewrite.c>
  RewriteEngine on
|improve this answer|||||
  • Thank you for quick response. I tried putting the rule in .htaccess. But it did not work. Can I copy the same code given above or some modification is required ? – Sandesh Yadav Jun 8 '13 at 7:32
  • Thank you very much for explaining more. My urls are the same as described above. But since it is a multilingual site, I tried with RewriteRule ^en/artists/([0-9]+)/.*$ en/artists/$1 as well as the above one . And also restarted apache. But its still not working. – Sandesh Yadav Jun 8 '13 at 8:04
  • It seems apache is not picking the rule. Because the simple rule also did not work. Could it be because I am already using URL alias from pathauto module ? – Sandesh Yadav Jun 8 '13 at 8:19
  • Deja vu: drupal.stackexchange.com/questions/55006/… (I am going to work this into my answer). – Free Radical Jun 8 '13 at 8:55
  • It worked after adding ?q= in the target as mentioned in your answer. Thank you very much for the constant support. :) – Sandesh Yadav Jun 8 '13 at 9:07

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.