htaccess redirect to file based on referrer

by swick
20 replies
how do I redirect all traffic to a specific file in my html directory based on referrer with .htaccess?

I've tried various lines and I can't get it to work.
#based #file #htaccess #redirect #referrer
  • Profile picture of the author lagrimas175
    Originally Posted by swick View Post

    how do I redirect all traffic to a specific file in my html directory based on referrer with .htaccess?

    I've tried various lines and I can't get it to work.
    It is possible although I am not sure what the use is that you are trying to make of .htaccess for redirectional purposes.
    {{ DiscussionBoard.errors[2442837].message }}
  • Profile picture of the author Sandy Hands
    Originally Posted by swick View Post

    how do I redirect all traffic to a specific file in my html directory based on referrer with .htaccess?

    I've tried various lines and I can't get it to work.
    I use a site called corz.org for a lot of htaccess tips (no affiliation).
    {{ DiscussionBoard.errors[2442892].message }}
  • Profile picture of the author ronperkins
    Originally Posted by swick View Post

    how do I redirect all traffic to a specific file in my html directory based on referrer with .htaccess?

    I've tried various lines and I can't get it to work.
    In the example below, we look for referer's from somesite.com and then redirect them to a page on your site called yoursite.html.

    Code:
    RewriteCond %{http_referer} ^http://([^.]+.)*(somesite).com
    RewriteRule ^$ yoursite.html [R=302,L]
    Please note for this to work you will need to have permission from your hosting company to use directives from the mod_rewrite module.

    Hope this helps...
    {{ DiscussionBoard.errors[2443055].message }}
    • Profile picture of the author swick
      -- the above syntax is not working



      Im trying to redirect based on if a referrer contains a certain string
      {{ DiscussionBoard.errors[2443094].message }}
      • Profile picture of the author ronperkins
        Originally Posted by swick View Post

        Im trying to redirect based on if a referrer contains a certain string
        In this case try this:

        Code:
        RewriteCond %{http_referer} yourstring [NC]
        RewriteRule ^$ webpage.html [R=302,L]
        {{ DiscussionBoard.errors[2443120].message }}
        • Profile picture of the author ronperkins
          swick, as I will be away from my pc for a bit... If you are still having problems it may indicate that mod_rewrite may not be active on your server. I would suggest deleting all mod_rewrite rules in your .htaccess file, and then trying this simple test:

          Code:
          Options +FollowSymLinks
          RewriteEngine on
          RewriteRule ^foo http://www.google.com/ [R=302,L]
          So when you go to your site followed by foo i.e. http://www.yoursite.com/foo you should be redirected to Google. If you are not then mod_rewrite is not active.
          {{ DiscussionBoard.errors[2443196].message }}
          • Profile picture of the author swick
            yea the test example works so I know rewrite is working, but the referer syntax still isn't working

            no idea
            {{ DiscussionBoard.errors[2443346].message }}
            • Profile picture of the author ronperkins
              if the last example worked... the code below should work also...

              Code:
              Options +FollowSymLinks
              RewriteEngine on
              RewriteCond %{HTTP_REFERER} yourstring [NC]
              RewriteRule /*$ http://www.yoursite.com/webpage.html [R=302,L]
              Also delete your browser cache...

              Does it work now?
              {{ DiscussionBoard.errors[2444041].message }}
              • Profile picture of the author swick
                I am getting the same error as with other syntax's that I tried:

                Firefox tells me:

                The page isn't redirecting properly

                Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

                * This problem can sometimes be caused by disabling or refusing to accept
                cookies.


                The address bar shows the correct url/page that Im trying to redirect to and when I click the address and hit enter the page loads fine, I just get that weird error when I come from the referral link that Im trying to redirect. Doesn't make any sense.

                And then Internet Explorer can't even load the page.
                {{ DiscussionBoard.errors[2445865].message }}
  • Profile picture of the author swick
    yea ok I thought it was a problem with my server, but I just tried it on 3 different servers and I am getting the same error in firefox.
    {{ DiscussionBoard.errors[2446014].message }}
  • Profile picture of the author Dimon003
    Yeah! It works! Thanks very much.
    {{ DiscussionBoard.errors[2447406].message }}
    • Profile picture of the author virginmind
      Hi swick,

      I suggest u gud redirection and 404 solving tool, if solve ur problem

      htaccessredirect.net

      Free .htaccess Redirect Generator


      Enjoy!!!
      {{ DiscussionBoard.errors[2451429].message }}
      • Profile picture of the author elan
        Hi Ronperkins,

        My requirement is same as Swick. I'm trying to redirect based on if a referrer contains a certain string. I have followed all the instructions that you mentioned here, but ended up with either 'redirect looping' or '500 internal server error'.

        Also I have done your 'mod_rewrite' active test and got 500 internal server error. Is this mean mod_rewrite is not active in my server?

        Also I'm trying to do this in a word press site. So you can see the following default HTACCESS code for permalink.

        # BEGIN WordPress
        <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule . /index.php [L]
        </IfModule>
        # END WordPress

        Is this conflicting with referrer redirect code? Following is the referrer redirect code that am using.

        Options +FollowSymLinks
        RewriteEngine on
        RewriteCond %{HTTP_REFERER} yourstring [NC]
        RewriteCond %{REQUEST_URI} !^/webpage.html
        RewriteRule /*$ webpage URL [R=302,L]

        Help me & Thanks in advance!
        {{ DiscussionBoard.errors[2918137].message }}
        • Profile picture of the author ronperkins
          Hi elan

          You need to add your rewrite rule before the one for Wordpress... try the test one again as follows:

          Code:
           
          <IfModule mod_rewrite.c>
          Options +FollowSymLinks
          RewriteEngine on
          RewriteRule ^foo Google [R=302,L]
          </IfModule>
           
          # BEGIN WordPress
          <IfModule mod_rewrite.c>
          RewriteEngine On
          RewriteBase /
          RewriteCond %{REQUEST_FILENAME} !-f
          RewriteCond %{REQUEST_FILENAME} !-d
          RewriteRule . /index.php [L]
          </IfModule>
          # END WordPress
          {{ DiscussionBoard.errors[2919671].message }}
          • Profile picture of the author elan
            Hi Ronperkins,

            Thanks for the reply. Well, that works which means mod_rewrite is enabled on my server. Now when I do the following referrer redirection its not working. Please help

            <IfModule mod_rewrite.c>
            Options +FollowSymLinks
            RewriteEngine on
            RewriteCond %{HTTP_REFERER} yourstring [NC]
            RewriteCond %{REQUEST_URI} !^/webpage.html
            RewriteRule /*$ website url [R=302,L]
            </IfModule>

            # BEGIN WordPress
            <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule . /index.php [L]
            </IfModule>
            # END WordPress
            {{ DiscussionBoard.errors[2935187].message }}
            • Profile picture of the author SteveJohnson
              Try this:
              Code:
              <IfModule mod_rewrite.c>
              Options +FollowSymLinks
              RewriteEngine on
              RewriteCond %{HTTP_REFERER} ^yourstring [NC]
              RewriteRule ^/$  /yourURI [L]
              </IfModule>
               
              # BEGIN WordPress
              <IfModule mod_rewrite.c>
              RewriteEngine On
              RewriteBase /
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule . /index.php [L]
              </IfModule>
              # END WordPress
              For the 'yourURI', use the portion of the URI after the domain name, i.e. http: //yourdomain.com /thispart.html

              Note that this is a rewrite - it's not a redirect, in other words, the browser location bar will reflect the requested URL. If you want to actually redirect, add the full URL including domain in place of '/yourURI' and add R=302 to the flag area inside the brackets (like you had before).
              Signature

              The 2nd Amendment, 1789 - The Original Homeland Security.

              Gun control means never having to say, "I missed you."

              {{ DiscussionBoard.errors[2935829].message }}
              • Profile picture of the author ronperkins
                elan, give this one a try if needed:

                Code:
                 
                <IfModule mod_rewrite.c>
                Options +FollowSymLinks
                RewriteEngine on
                RewriteCond %{HTTP_REFERER} yourstring [NC]
                RewriteRule /*$ http://www.google.com [R=302,L]
                </IfModule>
                 
                # BEGIN WordPress
                <IfModule mod_rewrite.c>
                RewriteEngine On
                RewriteBase /
                RewriteCond %{REQUEST_FILENAME} !-f
                RewriteCond %{REQUEST_FILENAME} !-d
                RewriteRule . /index.php [L]
                </IfModule>
                # END WordPress
                {{ DiscussionBoard.errors[2942831].message }}

Trending Topics