Apache mod-rewrite with get parameters - .htaccess

I now have this two rules which work ok
RewriteRule ^paymentok$ ?,return,payment&status=OK
RewriteRule ^paymentfail$ ?,return,payment&status=FAIL
What i need is also the get parameters after the paymentok or paymentfail, i.e. when this page is accessed:
paymentok?myvar1=dfsdf&myvar2=ewrwe
then the redirected URL should be:
?,return,payment&status=OK&myvar1=dfsdf&myvar2=ewrwe
Is this possible with rewrite rules?

YOu need to invlude the QSA flag in your rules:
RewriteRule ^paymentok$ ?,return,payment&status=OK [L,QSA]
RewriteRule ^paymentfail$ ?,return,payment&status=FAIL [L,QSA]
The QSA flag (Query String Append) appends any existing query string to the new one that you've created in your rule's target.

Related

Redirect a URL in .htaccess, but keep the query string

I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)
E.g.
https://mywebsite.example/service/viewinvoice.php?id=1660
I want to redirect it to:
https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660
So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...
I tried this below, without any success
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]
I think this is not the right solution.
Does someone has suggestions?
You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.
Your rule should be:
RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]

htacces rewrite - extend url without modifying query

I want to change:
/?q=bla
to
/search?q=bla
I have placed rule like:
RewriteRule ^search?q=(.*)$ /?q=$1 [L]
but it doesn't work, I would really appreciate some help, thanks
You can't match the query string using the pattern inside a RewriteRule. You need to match against the %{QUERY_STRING} var inside a RewriteCond:
RewriteCond %{QUERY_STRING} ^q=
RewriteRule ^search$ / [L,QSA]
Technically, you don't need the QSA flag, since query strings get appended automatically.

Redirect permanent with additional params

My goal is to redirect from
/kontaktaufnehmen/kontakt.html to /auftragsverwaltung/kontakt/?type=1
and the parameters must be added to the new path.
I tried
Redirect permanent /kontaktaufnehmen/kontakt.html /auftragsverwaltung/kontakt/?type=1
but the parameters are not added to the redirected path.
Without ?type=1 the parameter will be added.
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} !type=1
RewriteRule ^/?kontaktaufnehmen/kontakt.html /auftragsverwaltung/kontakt/?type=1 [L,R=301,QSA]
RewriteCond %{QUERY_STRING} type=1
RewriteRule ^/?kontaktaufnehmen/kontakt.html /auftragsverwaltung/kontakt/ [L,R=301]
The important thing here is the QSA flag, which means whatever query string that's already there gets appended. The mod_alias directive Redirect won't do this for you. The second rule is simply a redirect because it sees that the type=1 query string is already there so it won't add another.

.htaccess redirect to a directory and retain $_GET variables?

I want to redirect from /orders/whatever/?n=4
to:
/orders/?shopURL=whatever&n=4
I managed to redirect to the correct directory but I'm losing the passed variables.
This is my current rule:
RewriteRule ^([\w\d]+)$ /orders/?shopURL=$1 [L]
If you want to enforce the existence of a query string, use:
RewriteCond %{QUERY_STRING} (\bn=\d+\b)
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1&%1 [L]
Otherwise, you can use the following without a RewriteCond:
RewriteRule ^orders/([\w\d]+)/?$ /orders/?shopURL=$1 [L,QSA]
The QSA flag to RewriteRule will retain existing query string fields.
Your RewriteRule seems very generic, as though it would redirect almost every page.
I'm not that great with Regular Expressions so I'll leave those to you, or someone else. But you need something at the end of the rewrite rule to catch parameters, then append it at the end of the new URL.
RewriteRule ^([\w\d]+)/(somethinghere)$ /orders/?shopURL=$1&$2 [L]

Trying to create a rewrite rule in .htaccess

I have a url that looks like this
www.myurl.com/page/blog
which works fine with my current rewrite rule. But when I try to append append additional query stings to it it doesn't work.
www.myurl.com/page/blog?group=2
Is there something I could add to my rewrite rule that will make it catch all additional query strings?
This is my current rule:
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
You need to add the [QSA] flag to pass through the original query string params i.e.
RewriteRule ^page/([^/.]+)/?$ index.php?page=$1 [L,QSA]

Resources