Home > Net >  Replace ? with / in rewrite rules
Replace ? with / in rewrite rules

Time:09-08

I need to achieve this map given below :

https://www.domain1.com/open.html?cuSDyh&utm_medium=rep-email

to 

https://www.domain2.com/c/cuSDyh?utm_medium=rep-email

I write a rewrite rule to achieve this which seems wrong as "?" is not replaced in the end result.

RewriteRule ^/open.html?(.*)$ https://www.domain2.com/c/$1 [NC,R=301,L]

Can anyone help me to find the right rewrite rule for this one?

CodePudding user response:

RewriteRule ^/open.html?(.*)$ https://www.domain2.com/c/$1 [NC,R=301,L]

There are a couple of problems here that prevents the rule from working:

  • The RewriteRule pattern (1st argument) matches against the URL-path only, which notably excludes the query string. To match the query string you need an additional condition (RewriteCond directive) and match against the QUERY_STRING server variable.

  • The URL-path matched by the RewriteRule pattern does not start with a slash.

Note that the RewriteRule pattern is a regex. The unescaped ? in the above regex is a special meta character (a quantifier) that makes the preceding token optional. So, in your example, the l is optional. (Although you match everytihng that follows anyway, so the ? is entirely redundant.)

FROM: /open.html?cuSDyh&utm_medium=rep-email
TO:   /c/cuSDyh?utm_medium=rep-email

I'm assuming the first URL-parameter (eg. cuSDyh in your example) consists of letters only (lower or uppercase) and is not a name=value pair (as per your example). There can be 0 or more URL parameters that follow the first URL parameter.

Try the following instead:

RewriteCond %{QUERY_STRING} ^([a-z] )(?:&|$)(.*) [NC]
RewriteRule ^open\.html$ /c/%1?%2 [NE,R=302,L]

The %1 and %2 backreferences match the captured subgroups in the preceding CondPattern. ie. %1 matches the first URL parameter (eg. cuSDyh in your example) and %2 matches all subsequent URL params, excluding the delimiter.

The NE flag (not NC) is required on the RewriteRule directive to prevent the query string part of the URL being doubly URL-encoded in the redirect response. (The QUERY_STRING variable is already URL-encoded.)

If there are no additional URL parameters after the first then the above rule will essentially append an empty query string (denoted by the trailing ?). However, the trailing ? is removed automatically by Apache in the redirect response.

To avoid potential caching issues, always test first with a 302 (temporary) redirect and only change to a 301 (permanent) redirect - if that is the intention - once testing is complete.

  • Related