1

i would like to create a 301 from a page to the same page except that the destination page has a parameter in the url. Browsers show me an error (too many redirects) so its seems that there is an infinite loop. This is my code:

RewriteEngine on
Redirect 301 /index.php http://www.monsite.com/index.php?step=1

thanks to advice me for that :D

1
  • Please try and accept answers on some of your older questions... your accept rating is 20% and that discourages others from assisting you. Commented Jun 26, 2012 at 15:16

1 Answer 1

11

You need to conditionalize the redirect and do it in PHP to prevent the infinite redirect loop.

index.php:

if(!isset($_GET['step'])){
    header('Location:http://www.monsite.com/index.php?step=1');
}

The way you have it configured will redirect indefinitely, since nothing is saying to the engine "don't redirect me as soon as the URL variable step is set".

There are ways to do this in the .htaccess file, but since these kind of redirects are generally application logic it seems to make more sense to do it directly in your script.

Or, for a pure .htaccess solution:

#if query string is empty

RewriteCond %{QUERY_STRING} ^$

#match on / for root, or .index.php in request and send to query string version 

RewriteRule ^(/|index.php)?$ /index.php?step=1 [R=301,L]
Sign up to request clarification or add additional context in comments.

7 Comments

hi, i would like to only use htaccess... Thanks for your help
Thank you very much, this code redirects any page of the webpage to index.php?step=1, can you take a look please ?
I have it working on my environment, redirecting only index.html and root /. Are you sure you didn't forget the $ in the Rewrite rule? this ensures that the request string terminates directly after those strings. Can you give me the url that is being redirected wrong? Do you have other rewrite rules/conditions as well that might conflict?
The .htaccess contains only your code, and i copied it entierly... :|
Oh sorry i just understand the problem, in fact this is not the index.php but product.php that's why you understood that i want to redirect root. But i won't redirect root, only product.php to product.php?step=1. What i have to change (replacing index.php by product.php?) Sorry for that and thank you for your patience.
|

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.