Home > Back-end >  How set rule in .htaccess when argument=argument?
How set rule in .htaccess when argument=argument?

Time:05-10

For example:

RewriteCond %{QUERY_STRING} (?)argument=argument

but:

RewriteCond %{QUERY_STRING} (?)argumentext=argumentext

I find rule:

RewriteCond %{QUERY_STRING} (?)[a-zA-Z0-9_]=[a-zA-Z0-9_]

But this rule find only "=" and random text before and after, but i need when argument=value

Example:

IP - - [DATE] "GET /?value=value HTTP/1.1" 
IP - - [DATE] "GET /?random=random HTTP/1.1" 
IP - - [DATE] "GET /?qwerty=qwerty HTTP/1.1" 
IP - - [DATE] "GET /?randomvalues=randomvalues HTTP/1.1" 

Need a rule when they are equal

CodePudding user response:

You can use this rule with a capture group and a back-reference:

RewriteCond %{QUERY_STRING} ^([^=] )=\1$
RewriteRule ^ - [G]

Here RewriteRule ^ - [G] is used for testing only.

Explanation:

  • ^: Match start
  • ([^=] ): Match 1 of any character that is not = in capture group #1
  • =: Match a =
  • \1: Match same value as in capture group #1
  • $: End

CodePudding user response:

 "GET /?value=value HTTP/1.1" 
 "GET /?random=random HTTP/1.1" 
 "GET /?qwerty=qwerty HTTP/1.1" 
 "GET /?randomvalues=randomvalues HTTP/1.1"

You can do something like the following to match <something>=<something> where the parameter name and value are the same:

RewriteCond %{QUERY_STRING} (?:^|&)(\w )=\1(?:$|&)

Where \1 is an internal backreference that matches the URL parameter name.

This name=name pair can occur anywhere in the query string, amongst other URL parameters. But only whole parameter names will match.

  • Related