2

I realize this seems confusing, but let me give some background first ...

I have a node (say node/123) with a path alias (call it: /books). So if I go to http://example.com/books I'm viewing a page that's already been aliased by Drupal from /node/123.

However, now I put a Views block on that page and that block has an exposed filter. So, when I filter results I end up with an URL that looks something like so: http://example.com/books?type=fiction


Now here's the problem: I want to rewrite the above URL into something like http://example.com/fiction-books

This isn't as easy as it seems:

  • You can't use the pathauto module to set a new alias.
  • You can't use a function like hook_url_inbound_alter or even custom_url_rewrite_outbound because the querystring doesn't exist in any of those.

I attempted a rewrite in htaccess, which seems like the best solution, but I can't get anything there to work (I suspect this is because of Drupal rewriting the alias already so the pattern to use there is confusing).

For the record I know almost nothing about rewrites, but I've got this in htaccess which doesn't work:

RewriteCond %{REQUEST_URI}^/books$
RewriteCond %{QUERY_STRING}^type=fiction$
RewriteRule %{REQUEST_URI}^/fiction-books$ [L,R=301]

So, how do I do it? Advice on an internal Drupal-way would be ideal. But if not, then help via an htaccess RewriteRule would be really appreciated.

2 Answers 2

1

Have a look to Query Parameters To URL, I believe it fits your need :

This module provides the ability to rewrite URL query parameters into Clean URL components on specified paths.

An example of a dirty URL like that could be: example.com/events?field_category_id[0]=100&field_category_id1=101&field_author_name[0]=John

Using this module you can transform the URL into: example.com/events/p/field_category_id/0__100--1__101/field_author_name/John

1
  • 1
    Thanks! I did see this module but it actually doesn't fit the need (as far as I can tell) because I need custom aliases that have nothing to do with the existing url (compare the before and after urls I present in the question). This module basically just rewrites the querystring into something that looks more like a real url....my custom url will have nothing to do with the content of the querystring so I need to have full control to customize the output.
    – Feature
    Jun 5, 2015 at 13:18
0

Looking at your sample URL (= http://example.com/books?type=fiction) I think you should try to make the value (= fiction) of the URL argument (= type) available as a new variable to Rules. To do so, you can use 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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