Two question marks and two ampersand in query string? - .htaccess

Ech, my query string look's like:
http://localhost/index.php?page=public&another=http://www.google.com?omg=tt&nop=asd
And afcourse i rewrite it with regex to:
regex:
RewriteRule ^pass-([^=]*)=([^=]*)$ index.php?page=$1&another=$2 [L]
http://localhost/pass-url=http://www.google.com?omg=tt&nop=asd
(1)But then url becomes to: http://www.google.com only.
If i try urlencode with this url without regex:
http://localhost/index.php?page=public&another=http://www.google.com?omg=tt&nop=asd
it echo:
http%3A%2F%2Fwww.google.com%3Fomg%3Dtt
(2)In this case &nop=asd part gone.
So how to make (1) work and why (2) do this ?
The biggest question would be how to pass two question and ampersand in query string ?
Any suggestion regarding this situation ?

Problem is in your rewrite rule. Have it like this:
RewriteRule ^pass-([^=]*)=([^=]*)$ hw.php?page=$1&another=$2 [L,QSA]
Note that additional QSA flag which will make sure to preserve original query parameters.

Related

htaccess : How to replace string with special character?

I would like to redirect the URL :
https://www.example.com/blablabla,?trx_id=TX-23
to
https://www.example.com/blablabla,TX-23
Basically, I would like to remove the string : ?trx_id=
I tried the following but it it's not working. It's seems like it's related to special characters
RewriteRule ^(.+)?trx_id=(.+)$ $1$2 [R=301,L]
Can anyone help please ?
Thanks
It does not work because when the request is parsed by apache, it's split in several parts : host, port, path and query string. The directive RewriteRule matches against the url path which is : blabla, in your example, while the parameters are put in the query string trx_id=TX-23 (question mark removed).
To match against the query string, you have to use a condition, like this.
RewriteCond %{QUERY_STRING} ^trx_id=(.+)$
RewriteRule ^(.+) /$1%1 [R=301,QSD,L]
The condition only affects its following rule. Back references such as $N refer to the rule pattern, and %N refer to the condition pattern.
Also note the QSD flag to discard the initial query string, otherwise it would be kept in the rewritten request (at least in apache 2.4, note sure for apache 2.2).

htaccess RewriteRule with literal question marks (not query string)

I need to be able to match question marks because there was a translated text encoding mistake, and part of the URL ended up hardcoded with question marks in them. Here's a URL example that I need to rewrite:
https://example.com/Documentation/Product????/index.html
Here is my current rewrite rule. It works when the characters following "Product" are not question marks, but when they are, the rule doesn't apply.
RewriteRule "^Documentation/Product[^/]+/(.*)$" "https://s3.amazonaws.com/company-documentation/Help/Product/$1" [L,NC]
How would I make sure that question marks are considered to be characters too in this rule? I can't expect that only question marks and not the original non-English characters will be in the URL, so I want the rule above to match both question marks and any other character.
I found this topic which seems relevant, but the flags don't help, and the answer doesn't explain how to overcome the problem mentioned in the "Aside".
https://webmasters.stackexchange.com/questions/107259/url-path-with-encoded-question-mark-results-in-incorrect-redirect-when-copied-to
https://example.com/Documentation/Product????/index.html
You say it's "not a query string", but actually that is exactly what it is. And that is why you can't match it with the RewriteRule pattern. The above URL is split as follows:
URL-path: /Documentation/Product (matched by the RewriteRule pattern)
Query string: ???/index.html (note 3 ? - the first one starts the query string)
To match the query string you'll need an additional RewriteCond directive that checks against the QUERY_STRING server variable.
For example, to match the above URL, you would need to do something like:
RewriteCond %{QUERY_STRING} ^\?*/index\.html
RewriteRule ^Documentation/Product$ https://s3.amazonaws.com/company-documentation/Help/Product/index.html [NC,R,L]
This matches any number of erroneous ? at the start of the query string.
I've added the R (redirect) flag. Your directive (without the R flag) would trigger an external redirect anyway (because you specifying an absolute URL in the substitution), but it is far better to be explicit here. This is also a temporary (302) redirect. If this should be permanent (301) then change it to R=301, but only once you have confirmed that it's working OK (301s are cached hard by the browser so can make testing problematic).
UPDATE:
...so I want the rule above to match both question marks and any other character.
Only if there are question marks in the URL will there be a query string, so I think it is advisable to keep these two rules separate.
If there could be any erroneous characters at the start of the query string and if you want to capture the end part of the URL (like you are doing in your original directive, eg. index.html) then you can modify the above to read:
RewriteCond %{QUERY_STRING} /(.*)$
RewriteRule ^Documentation/Product$ https://s3.amazonaws.com/company-documentation/Help/Product/%1 [NC,R,L]
Note the %1 (as opposed to $1) backreference in the substitution string. This is a backreference to the captured group in the last matched CondPattern (ie. /(.*)$).
You can follow this with your existing directive (but remember to include the R flag) for more "normal" URLs that don't contain a ? (ie. query string).
NB: Surrounding the arguments in double quotes are entirely optional in this example. They are only required if you have unescaped spaces in the pattern or substitution arguments.
In summary
# Redirect URLs of the form:
# "/Documentation/Product?<anything#1>/<anything#2>"
RewriteCond %{QUERY_STRING} /(.*)$
RewriteRule ^Documentation/Product$ https://s3.amazonaws.com/company-documentation/Help/Product/%1 [NC,R,L]
# Redirect URL-paths of the form (no query string):
# "/Documentation/Product<something>/<anything>"
RewriteRule ^Documentation/Product[^/]+/(.*) https://s3.amazonaws.com/company-documentation/Help/Product/$1 [NC,R,L]

mod_rewrite pass variable and

What I would need to do is to pass 1 variable as variable, and the rest as a rest of URL intact, so I can get them by $_GET in php later. The below does not work:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html?(.*)/?$ store.php?var1=$1&$2 [L]
Possible Links could be:
store/products.html
store/products.html?sort=asc&price=down
store/products.html?price=down&here_we_can_have_a_lot_of_different_params_in_whatever_order
Basically, just take this $var1 and the rest forward to URL? How can I do that?
P.S. I think I found a solution:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html?(.*)/?$ store.php?var1=$1&%{QUERY_STRING} [L]
The problem is your premise as to how a RewriteRule works. A RewriteRule only matches against the URI path (in apache terms the Request URI) and not against the query string. This means the (.*)/?$ at the end of your regex only works because it can reduce to just '$' (or the end of the string) and the ? before it just means the regexp will match .htm as well as .html
The simpler version of your rule is as follows:
RewriteRule ^store/([a-zA-Z0-9\_\-]+).html store.php?var1=$1 [L,QSA]
The QSA stands for Query String Append, which simply adds back any existing query string to the rewritten URL.

Can I use htaccess to Remove a phantom ? from the URL

Google is somehow indexing some phantom URL's for my website. I'm still trying to find the problem.
For example, everything in bold below is NOT supposed to be there.
http://www.mydomain.com/directory/sights?/kauai/sights/sights/kalalau_lookout
It should look like this:
http://www.mydomain.com/directory/sights
How can I strip out the ? mark in addition to everything after it and make it 301 redirect to the proper page?
Thanks,
John
From mod_rewrite
Modifying the Query String
By default, the query string is passed through unchanged.
...
When you want to erase an existing query string, end the substitution string with just a question mark.
...
You can remove all query strings with
RewriteEngine On
RewriteCond %{QUERY_STRING} .
RewriteRule .* http://www.mydomain.com$0?

301 Htaccess RewriteRule Query_String

Problem: Visitors open the url website.com/?i=133r534|213213|12312312 but this url isn't valid anymore and they need to be forwarded to website.com/#Videos:133r534|213213|12312312
What I've tried: During the last hours I tried many mod_rewrite (.htaccess) rules with using Query_String, all failed. The last message in this topic shows a solution for this problem, but what would be the rule in my situation.
I'm very curious how you would solve this problem :)!
The following will handle the simple case you show. You'll need to add additional logic if you need to allow for other parameters in the query string or file names before the ?.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^i=(.*)
RewriteRule ^.* /#Video:%1? [NE,R=permanent]
Why is this tricky?
RewriteRule doesn't look at the query string, so you have to use RewriteCond to evaluate the QUERY_STRING variable and capture the part you'll need later (referenced via %1)
the hash character (#) is normally escaped, you must specify the [NE] flag
The trailing ? on the substitution string is required to suppress the original query string
I tested this on Apache 2.2.

Resources