user3146987
user3146987

Reputation: 51

Redirect Unique URL to Different URL

so I have been fiddling around with an idea and I cant seem to get it working.

I have a game where people share my website, each share gets their unique code -

mydomain.com/?1234567

What I want to achive is that if a person directly visits my website

mydomain.com he/she will stay on that page, but if the user comes from a unique 7 number code then he/she would be sent to anotherdomain.com

I tried using .htaccess but was only able to not redirect unique url, while redirecting main domain, exactly the opposite what I wanted.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com/?$1 [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/ [R=301,L]

How to resolve this matter?

Upvotes: 1

Views: 135

Answers (2)

Amit Verma
Amit Verma

Reputation: 41219

Try :

RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteCond %{QUERY_STRING} ^[0-9]{7}$
RewriteRule ^(.*)$ http://anotherdomain.com/ [R=301,L]

The regular expression above - ^[0-9]{7}+$ will match any numbers with maximum length 7 in query string ,if it matches then The url will redirect to a new location.

Upvotes: 1

irfanengineer
irfanengineer

Reputation: 1300

Try this:

$url =  $_SERVER['REQUEST_URI'];
$array = explode('/', $url);
$var = $array[1];

if(strlen($var) == 7){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://anotherdomain.com");
  exit;
}

Upvotes: 1

Related Questions