0

I've been searching for hours on how to do this and feel as if I'm close.

I am trying to redirect specific visitors to a different page on my site based on the string parameters passed in the URL.

For exmaple, I want the URL mysite.com/index.php?src=msn&test=1 to be redirected to mysite.com/msn/index.php?src=msn&test=1

I am planning on my normal site visitors to access the index.php page without being re-directed, so it should only redirect when the query string matches the value I set.

Here is my code:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^src=msn(.*)$
RewriteRule ^(.*)$ http://www.mysite.com/msn/ [L]

The above code works when I go to the URL mysite.com/?src=msn&test=1 but it does not work for mysite.com/index.php?src=msn&test=1. Any idea how I can get it to work when I enter index.php?

Thanks.

EDIT:::

I also have the following rules in my .htaccess file:

 ErrorDocument 404 /error404.php

 <IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteBase /blog/
 RewriteRule ^index\.php$ - [L]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule . /blog/index.php [L]
 </IfModule>

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

The first is a 404 redirect, second is a wordpress file change, and the last forces all pages to be http://www.

2
  • 1
    Do you have any other rules in your htaccess? Commented Jan 24, 2012 at 19:25
  • Ulrich, I've updated the original post to show the other rules in .htaccess file. Commented Jan 25, 2012 at 14:03

1 Answer 1

1

This line

RewriteRule ^index.php$ - [L]

prevents index.php from further processing.

The easiest solution is to move your new rules to the top as below ( I assume they are all in the same .htaccess file)

ErrorDocument 404 /error404.php

Options +FollowSymlinks
RewriteEngine on
RewriteBase /blog/

#rule to add www to domain
RewriteCond %{HTTP_HOST} ^mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.com/$1 [R=301,L]

#new rule to redirect based on query string param
RewriteCond %{QUERY_STRING} ^src=msn(.*)$
#unless it is already msn
RewriteCond %{REQUEST_URI} !^/msn/ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/msn/ [L,R]

#rules for blogs
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that makes sense now. I apologize for my basic understanding of .htaccess, but the problem I have now is I cannot prevent the rule from applying to the page I'm re-directing to and thus I'm entering into an infinite loop. Any idea how to fix this?
@user1167727 Added an extra condition to prevent this above

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.