Note that there are some explanatory texts on larger screens.

plurals
  1. POmod_rewrite: Pass the path & query string URL as a parameter
    text
    copied!<p>I'm using mod_rewrite to rewrite pretty URLs to a form supported by a Spring 2.5 application.</p> <pre><code>e.g. /category/cat1?q=x =&gt; /controller?category=cat1&amp;q=x </code></pre> <p>However from my controller I want to know the original URL the request came from (so I can generate a link if required). This approach is needed generically across all pages so it is difficult to hard code.</p> <p><em>How can I access the original path + query string from my controller?</em></p> <p>I have tried using $0 to include the full path but this doesn't include the query string. I can't just append the path and the query string as this would result in some parts of the path being added as parameters <code>/category/cat1?category=cat1&amp;q=x</code> Note the addition of the unwanted <code>&amp;category=cat1</code> parameter, this causes the URL to no longer match that sent from the browser.</p> <p>I'm hoping mod_rewrite will let me reference the full URL and encode it as a parameter so my rule could look like:</p> <pre><code>RewriteRule /category/(.+) /controller?category=$1&amp;_originalUrl=${escape:$0}?${escape:&lt;original query string&gt;} [QSA] </code></pre> <p>Using my original example the end result passed through to my controller would be:</p> <pre><code>/controller?category=cat1&amp;_originalUrl=%2Fcategory%2Fcat1%3Fsearch%3Dx&amp;search=x </code></pre> <p>The important part is the value of <code>&amp;_originalUrl</code> which should be <code>%2Fcategory%2Fcat1%3Fsearch%3Dx</code> which in its unescaped version is <code>/category/cat1?q=x</code> (the original request URL that was sent from the browser).</p> <p>Any suggestions welcome, thanks in advance!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload