2

Have a problem with traffic coming from Google Ads.

A have a redirect roll that redirects

https://example.com/some-words-words-aktivitet/

to

https://example.com/index.php?mobile=1

(RewriteRule ^some-words-words-here//?$ ?mobile=1)

Now when I have the URL that includes gclid I need to pass that value to the final URL like this.

https://example.com/some-words-words-aktivitet/?gclid=some_value

to

https://example.com/index.php?mobile=1&gclid=some_value

How do I do that?

I have tried this with no success.

RewriteRule ^escape-room-teambuilding-aktivitet/\?gclid=([^/]*)/?$ ?mobile=1&gclid=$1
1
  • The rule you've processed is an internal rewrite (as it should be), not a "redirect" (as you've stated), so I'm wondering where the problem is here... Google Adwords processes the request using JS and if this is an internal rewrite then the gclid is still present in the visible URL that Adwords sees. (?) Adwords does not see the rewritten URL that contains the mobile URL parameter (and no gclid param) since this is entirely internal to your server. (?)
    – MrWhite
    Mar 29 at 12:06

1 Answer 1

0
RewriteRule ^escape-room-teambuilding-aktivitet/\?gclid=([^/]*)/?$ ?mobile=1&gclid=$1

The RewriteRule pattern matches the URL-path only, which notably excludes the query string, so the above will never match.

([^/]*) is also the wrong regex to use here since this is part of the query string, where parameters are delimited by &, not / (as in the URL-path).

You could probably just use the QSA (Query String Append) flag to append the original query string from the request to the substitution string. This will include the gclid parameter, but will also include any other URL parameters. For example:

RewriteRule ^example/$ index.php?mobile=1 [QSA,L]

Note the addition of index.php in the substitution string. Your original directive is reliant on mod_dir. And the L flag to prevent further processing.

This would rewrite a request of the form /example/?gclid=somevalue&foo=bar to index.php?mobile=1&gclid=somevalue&foo=bar.


Alternatively...

If, however, you wanted to target the gclid URL parameter only (and discard all other URL parameters) then you would need a separate condition and check against the QUERY_STRING URL parameter. For example:

RewriteCond %{QUERY_STRING} (?:^|&)(gclid=[^&]+)?
RewriteRule ^example/$ index.php?mobile=1&$1 [L]

This would replace your existing rule since it handles URLs both with and without the gclid URL parameter.

This would rewrite a request of the form /example/?gclid=somevalue&foo=bar to index.php?mobile=1&gclid=somevalue. And /example/?foo=bar to index.php?mobile=1& (the trailing & is inconsequential).


Aside:

RewriteRule ^some-words-words-here//?$ ?mobile=1

This doesn't match the stated example URL (so make sure you are editing the correct rule!). The trailing //?$ is also a bit odd. This is the same as simply /$ (a trailing slash is mandatory) because multiple contiguous slashes in the URL-path are reduced in the URL-path that is matched by the RewriteRule pattern. You are also missing the L flag, so the outcome is unpredictable if you have other directives that follow. (And, as mentioned above, you should be rewriting directly to index.php, not relying on the DirectoryIndex (mod_dir)).

3
  • Thank you @MrWhite unfortunately your solution did not work :-( Mar 29 at 15:30
  • @user3699041 After writing my answer I'm a bit confused as to what problem you are trying to solve here (hence my comment below your question). Ordinarily gclid is a URL parameter read by client-side JavaScript, so any internal rewriting of the request (as I assume you are doing here - not a "redirect") should not affect this to begin with. So, I'm wondering if there is something else going on here? Please update your question with your complete .htaccess file and the actual URL being requested. Are you doing something with the gclid parameter server-side?
    – MrWhite
    Mar 29 at 16:48
  • 1
    @user3699041 Aside: Strictly speaking you shouldn't need to include the gclid parameter in the rewritten URL anyway, since this could be parsed from the $_SERVER['REQUEST_URI'] PHP superglobal. I am assuming you are not externally "redirecting" the request (as you have erroneously stated in your question)?
    – MrWhite
    Mar 29 at 16:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.