0

Ok so I have a url like this:

http://example.com/admin/test.php?action=edit&id=2

However I need to add in a rule to redirect this to another url with a query string like this:

http://example.com/admin/login.php?redirect=test.php%3Faction%3Dedit%26id%3D2

I added a rule like this:

RewriteRule ^admin/(.*)?$ /login.php?redirect=/admin/$1?%{QUERY_STRING} [R,L]

And it works for the most part but there is one problem, the query string is not being escaped so it is thinking those are other $_GET variables. It looks like this:

http://example.com/admin/login.php?redirect=test.php%3Faction=edit&id=2

So the redirect is reading as test.php?action=edit. It is missing the id=2 because it is reading it as another $_GET variable instead of part of the $_GET['redirect'].

Is it possible to escape the %{QUERY_STRING} ? I have googled everything I can think of and cannot find an answer anywhere.

2
  • where are you redirecting because you might want to add something like location("header: login.php? " . encodeuri(current url)); to your page that does the redirect. Then if redirect parameter is sent and login is ok use header decodeuri(redirect) Commented Jul 9, 2013 at 22:39
  • I dont have the option to do this. I am working in wordpress and it is coming in an email that is automatically sent. So it sends that url but I need to redirect it to login with the redirect as a parameter. Commented Jul 9, 2013 at 22:45

1 Answer 1

1

Well, if you don't mind having a bunch of &'s at the end of your encoded URL, you can use optional captures and encode the & by hand. Something like:

RewriteCond %{QUERY_STRING} ^([^&]+)(?:&(.*)|)$
RewriteRule ^admin/(.*)?$ /login.php?redirect=/admin/$1\%3F%1\%26%2 [L,NE,R]

So if you go to:

http://example.com/admin/test.php?a=1&b=2

you get redirected to:

http://example.com/login.php?redirect=/admin/test.php%3Fa=1%26b=2

However, going to:

http://example.com/admin/test.php?a=1

will redirect you to:

http://example.com/login.php?redirect=/admin/test.php%3Fa=1%26

and your php script will extract the $_GET['redirect'] parameter as: /admin/test.php?a=1&. As long as you don't mind having that stray ampersand at the end.

If you have a lot of possible variables, and don't care about the ampersand, just add more (?:&(.*)|) to the end of the querystring condition and match them with backreferences (example, 4):

RewriteCond %{QUERY_STRING} ^([^&]+)(?:&(.*)|)(?:&(.*)|)(?:&(.*)|)$
RewriteRule ^admin/(.*)?$ /login.php?redirect=/admin/$1\%3F%1\%26%2\%26%3\%26%4 [L,NE,R]
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the response but I am unsure of how many can be in there. There might even be none. I was hoping there was a bit of a cleaner solution for this.

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.