In an upgrade scenario I have to provide some alias URLs like www.server.com/reading_23.html that should actually deliver www.server.com/reading/23.

I spent lots of hours trying to make that work by adding a line in .htaccess:

RewriteRule reading_(.*).html reading/$1

I have put that line into the standard D8 .htaccess, right after RewriteBase /. Unfortunately none of my attempts worked. The /reading is a path that's provided by one of my modules.routing.yml.

EDIT, 18.1. This seems to be very special for Drupal because it just doesn't work with the existing D8 redirects. I can only make a redirection ([L,R]) work but I don't want my end-users to see the reading/23 path in their browser. So actually the question is how to make URLs like reading_23.html possible with Drupal, the routing parameters can only live as separate parts of the path (like reading/{number} is ok but reading_{number}.html is bad).

Anyone who knows how to do this?

I do not want to create aliases in my Drupal DB for all node-Ids.

[EDIT Nr 2. 18.Jan 2017]

The Solution is an inbound Path Processor like 4k4 wrote. Thanks a lot.

[END EDIT]

closed as off-topic by Pierre.Vriens, DRUPWAY, kiamlaluno Jan 17 '17 at 17:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions on programming, PHP, SQL, etc. that do not relate directly to Drupal are off-topic here, but can be asked on Stack Overflow." – Pierre.Vriens, DRUPWAY, kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • I think your issue is you're trying to Rewrite a URL to a URL rewrite. After you rewrite reading_$1.html into reading/$1, that then is rewritten to index.php?q=reading/$1. So you shouldn't rewrite to reading/$1, instead try to rewrite it straight into index.php so it doesn't go through the second pass. – Beebee Jan 18 '17 at 16:51

I think your rewrite collides with the other rewrites in drupal standard .htaccess. Try a redirect. The redirect will be executed right away and the rest of .htaccess will no longer be processed. And it will also tell search engines that the url has changed.

RewriteRule reading_(.*).html reading/$1 [L,R=301]
  • Hm, I wanted to keep the reading_23 URL in the users Browser. When I redirect [R] the new URL pops up... – Rainer Feike Jan 18 '17 at 12:40
  • Use an inbound path processor. If you change the path here, it will not be visible in the browser. Example from core for the front page: api.drupal.org/api/drupal/… – 4k4 Jan 18 '17 at 12:49
  • Fantastic, the inbound path processor is the solution I was looking for! – Rainer Feike Jan 18 '17 at 19:25

Rewrites this code for your problem in .htaccess. The code will work for any string in between reading_ and .html

  RewriteEngine On
  RewriteRule ^reading_(.*)\.html$ http://www.server.com/reading/$1 [L,R=301]

Hope, this works fine for you.

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