1

I'm encountering a problem with my .htaccess file and with my limited knowledge of PHP I cant seem to figure this one out.

Goal: I want to redirect traffic that goes to my main page but has a query string, to be redirected to a different page with the query string intact. Like so:

https://www.mywebpage.com/?wd8RL4T6Q00&17.07&52LJqY-emLU&0

to

https://www.mywebpage.com/player.html?wd8RL4T6Q00&17.07&52LJqY-emLU&0

The user can only see the first and underlying pages like (https://www.mywebpage.com/howto.html) still have to be reachable.

In pseudo code (but somewhat resembling javascript ;) my solution would be something like:

if (querystring) {
  redirect to './player.html' + querystring
}

What I have tryd:

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule player.html  %1

But this seemed to redirect all traffic somehow.. Thanks for your help in advance!

1 Answer 1

1

Your current RegExp just matches everything. That's what the .* means. Instead, if you want the query string to have at least 1 character, you can change it to .+. I think that solves your current problem.

Then, for the redirect:

RewriteEngine On
RewriteCond %{QUERY_STRING} (.+)
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule .* /player.html [L]
Sign up to request clarification or add additional context in comments.

10 Comments

Hey Daan, thanks! That works! Now it selects only the links that have a query string in them. RewriteCond %{QUERY_STRING} (.+)
Great! If it's the solution to your problem, please accept my answer :)
Well, it is for the first part.. but not for the second part. I guess this line is right now: RewriteCond %{QUERY_STRING} (.+) So it rewrites only the links that have a query string in them. But not this line: RewriteRule player.html %1 So it still comes up with a 500 error. Do you know what I can do to refer it to the right page with the query string attached?
I've added to my answer. Is this what you hoped to achieve?
Hey Daan, thanks for your anwswer again! I'm getting a ERR_TOO_MANY_REDIRECTS. Maybe it picks up on the query string again when redirecting to /player.html?wd8RL4T6Q00&17.07&52LJqY-emLU&0 ? Don't know if the .htaccess file is read again after the redirect. Also the URL visible to the user changes now to: mywebpage.com/player.html?wd8RL4T6Q00&17.07&52LJqY-emLU&0. That is not supposed to happen. It should say: mywebpage.com/?wd8RL4T6Q00&17.07&52LJqY-emLU&0
|

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.