I'm using mod_rewrite to rewrite pretty URLs to a form supported by a Spring 2.5 application.
e.g. /category/cat1?q=x => /controller?category=cat1&q=x
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.
How can I access the original path + query string from my controller?
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 /category/cat1?category=cat1&q=x
Note the addition of the unwanted &category=cat1
parameter, this causes the URL to no longer match that sent from the browser.
I'm hoping mod_rewrite will let me reference the full URL and encode it as a parameter so my rule could look like:
RewriteRule /category/(.+)
/controller?category=$1&_originalUrl=${escape:$0}?${escape:<original query string>}
[QSA]
Using my original example the end result passed through to my controller would be:
/controller?category=cat1&_originalUrl=%2Fcategory%2Fcat1%3Fsearch%3Dx&search=x
The important part is the value of &_originalUrl
which should be %2Fcategory%2Fcat1%3Fsearch%3Dx
which in its unescaped version is /category/cat1?q=x
(the original request URL that was sent from the browser).
Any suggestions welcome, thanks in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…