1

I am trying to figure out how to use the ampersand symbol in an url.

Having seen it here: http://www.indeed.co.uk/B&Q-jobs I wish to do something similar.

Not exactly sure what the server is going to call when the url is accessed.

Is there a way to grab a request like this with .htaccess and rewrite to a specific file?

Thanks for you help!

Lucas
  • 73
  • 1
  • 8
  • What have you tried so far? Have you looked at [Characters allowed in a URL](http://stackoverflow.com/q/1856785/1779906). – Perleone Feb 06 '13 at 19:24

2 Answers2

2

Ampersands are commonly used in a query string. Query strings are one or more variables at the end of the URL that the page uses to render content, track information, etc. Query strings typically look something like this:

http://www.website.com/index.php?variable=1&variable=2

Notice how the first special character in the URL after the file extension is a ?. This designates the start of the query string.

In your example, there is no ?, so no query string is started. According to RFC 1738, ampersands are not valid URL characters except for their designated purposes (to link variables in a query string together), so the link you provided is technically invalid.

The way around that invalidity, and what is likely happening, is a rewrite. A rewrite informs the server to show a specific file based on a pattern or match. For example, an .htaccess rewrite rule that may work with your example could be:

RewriteEngine on
RewriteRule ^/?B&Q-(.*)$ /scripts/b-q.php?variable=$1 [NC,L]

This rule would find any URL's starting with http://www.indeed.co.uk/B&Q- and show the content of http://www.indeed.co.uk/scripts/b-q.php?variable=jobs instead.

For more information about Apache rewrite rules, check out their official documentation.

Lastly, I would recommend against using ampersands in URLs, even when doing rewrites, unless they are part of the query string. The purpose of an ampersand in a URL is to string variables together in a query string. Using it out of that purpose is not correct and may cause confusion in the future.

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
1

A URI like /B&Q-jobs gets sent to the server encoded like this: /B%26Q-jobs. However, when it gets sent through the rewrite engine, the URI has already been decoded so you want to actually match against the & character:

Rewrite ^/?B&Q-jobs$ /a/specific/file.html [L]

This makes it so when someone requests /B&Q-jobs, they actually get served the content at /a/specific/file.html.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220