Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>RewriteRule</code> does not work with query string directly -- you have to use <code>RewriteCond</code> for that.</p> <p>Here is the rule -- it will redirect (301 Permanent Redirect) ANY URL that has more than 1 parameter in query string and 1 of them is <code>ref</code></p> <pre><code>RewriteEngine On RewriteCond %{QUERY_STRING} (^|&amp;)ref=([^&amp;]*)(&amp;|$) RewriteCond %{QUERY_STRING} !^ref=([^&amp;]*)$ RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI}?ref=%2 [R=301,L] </code></pre> <p>For example:</p> <ol> <li><p>It will redirect <code>http://www.example.com/hello.php?id=86&amp;name=John&amp;ref=12d34888&amp;me=yes</code> to the same URL but with <code>ref</code> parameter only: <code>http://www.example.com/hello.php?ref=12d34888</code>.</p></li> <li><p>It will do nothing if only <code>ref</code> parameter is present or no parameter at all, e.g. <code>http://www.example.com/hello.php?ref=12d34888</code> or <code>http://www.example.com/hello.php</code>.</p></li> </ol> <hr> <p>If such redirect should only work for website root hits, then change the <code>RewriteRule</code> line to this:</p> <pre><code>RewriteRule ^$ http://%{HTTP_HOST}/?ref=%2 [R=301,L] </code></pre> <p>(this is if placed in .htaccess file in website root folder -- if placed in server config / virtual host context the rule needs to be slightly tweaked).</p> <p><code>http://www.example.com/?id=86&amp;name=John&amp;ref=12d34888&amp;me=yes</code> -> <code>http://www.example.com/?ref=12d34888</code></p> <hr> <p>If it has to be redirected to another domain, then replace <code>%{HTTP_HOST}</code> by domain specific name, e.g:</p> <pre><code>RewriteRule ^$ http://www.exampe.com/?ref=%2 [R=301,L] </code></pre> <p><strong>It all has been tested before posting.</strong></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload