Mod_rewrite: pass the URL of the path and query string as parameter

I use mod_rewrite to rewrite beautiful URLs into a form supported by Spring 2.5.

e.g. /category/cat1?q=x   =>  /controller?category=cat1&q=x

However, from my controller I want to know the source URL from which the request came (so I can create a link if necessary). This approach is necessary in the general case for all pages, so it is difficult to hard code.

How can I access the source line of the path + request from my controller?

I tried using $ 0 to include the full path, but this does not include the query string. I canโ€™t just add the path and query string, as this will add some parts of the path as parameters. /category/cat1?category=cat1&q=xPay attention to adding an unwanted parameter &category=cat1, this leads to the fact that the URL no longer matches the one specified from the browser.

I hope that mod_rewrite will allow me to refer to the full URL and encode it as a parameter, so that my rule looks like this:

RewriteRule /category/(.+)
            /controller?category=$1&_originalUrl=${escape:$0}?${escape:<original query string>}
            [QSA]

Using my original example, the final result passed to my controller will look like this:

/controller?category=cat1&_originalUrl=%2Fcategory%2Fcat1%3Fsearch%3Dx&search=x

The important part is the value &_originalUrlthat should be %2Fcategory%2Fcat1%3Fsearch%3Dx, which is in its unexpressed version /category/cat1?q=x(the source URL of the request sent from the browser).

, !

+3
3

RewriteCond, RewriteRule . . , .

QSA, :

RewriteRule ^/category/([^/]+)$ /controller?category=$1 [QSA]

: URI, ( THE_REQUEST):

RewriteCond %{THE_REQUEST} ^[A-Z]+\ ([^\s]+)
RewriteRule ^/category/([^/]+)$ /controller?category=$1&_originalUrl=%1 [QSA]

.

+8

.

RewriteCond %{query_string} ^q=([^&]+)
RewriteRule ^/category/(cat1)$ /controller?category=$1&q=%1

.

+1

, ? , , , PHP (urgh, dont ask), , - REQUEST_URI (http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html)

rewrite (http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)

:

ReWriteRule /category/(cat1)?q=(x) /controller?category=$1&q=$2 [E=FOO:....]

( , - )

0

Source: https://habr.com/ru/post/1725310/


All Articles