Forum Moderators: phranque

Message Too Old, No Replies

redirect and keep url query string?

         

1nD3X

5:59 pm on Jun 1, 2010 (gmt 0)

10+ Year Member



Hey party people.

I am not quite sure whether this is a silly question but I haven't been able to find a suitable explanation or example for it, so I thought I'd ask in any case...

I would like to know whether it is possible to redirect all page requests to the index.php file but keep the query string so that it's accessible inside the index.php, if that makes sense!?

For example, if someone types into their browser's address bar or follows the link www.mysite.com/index.php?option=com_content&view=article&id=39&Itemid=169 (yes, it's a Joomla site), I would love to redirect that to the index.php where I need to mainly check for or set cookies and/or have users agree to terms and conditions etc., but after all of that is done, direct them to the actual article/page they were trying to access.

I hope what I am trying to achieve is clear enough. And possible, for that matter.

I'd appreciate any useful response.

Thanks a lot!

jdMorgan

7:13 pm on Jun 1, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's possible. In fact, it is the default behaviour.

The only 'trick' you need to be aware of is that if you want to redirect page 'a' to page 'b', and then return to 'a', then you must tell the server under what conditions it should do that a-to-b redirect, and also under what conditions it should not. Otherwise, you'll have yourself a never-ending infinite redirection loop.

In this case, redirect from the initially-requested page to the 'login and set cookie' page if the cookie is not already set. Then set the cookie and redirect back to the initially-requested page. The redirect code will see that the cookie is now set, and do nothing.

mod_rewrite's RewriteCond can test %{HTTP_COOKIE} against a regular-expressions pattern to implement this function.

An alternative is to 'do everything inside your script' which eliminates the need for at least one of these redirects, and will therefore allow your site to 'run faster' as perceived by users.

Jim

1nD3X

9:27 pm on Jun 1, 2010 (gmt 0)

10+ Year Member



Hi Jim,

thanks for your response! Good news in general :)

Yes, I think I'd prefer to let the 'script' do the second round of redirecting. If I can steal some more of your time, what would my rewrite rule look like? Also, will what I want interfere with robots and such? And to push my luck a little further, will I be able to access the query string as per usual using PHP?

Thanks again for your help. Very much appreciated!

1nD3X

8:07 am on Jun 2, 2010 (gmt 0)

10+ Year Member



Hi again,

I've searched some more... Found someone with a similar goal, he used the following to redirect everything to root (he placed his .htaccess into the Joomla directory).

RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://site.com/
RewriteCond %{HTTP_REFERER} !^http://www.site.com/
RewriteRule /* [site.com...] [R,L]

I've tried it out but am not able to pull the query string from the URL... Probably one of those [R,L] guys that is preventing that?

Sorry for being persistent :)

g1smd

9:17 am on Jun 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The /* pattern would only match a request for example.com// or for example.com/// or for example.com/// or example.com//////////// etc.

The [R,L] flag will give you a 302 redirect.

Why are you testing the referrer? Some browsers don't send a referrer, and searchengine bots certainly do not. That variable will often be blank.

1nD3X

11:23 am on Jun 2, 2010 (gmt 0)

10+ Year Member



Thanks g1smd. That then isn't much use to me... I very much appreciate your help but I am not winning here. Let me try to explain in different terms what I want one more time and then I'll go bother someone else :)

In essence, I have a Joomla installation that I need to 'build a wall around' and have any requests first pass through the index.php outside of Joomla (one directory up), with query strings intact so that the index.php can redirect to the originally requested page once it has performed and passed some checks. Once inside the Joomla site, though, requests shouldn't go to the index.php in the directory above anymore.

Voodoo for sure.

Thanks.

g1smd

1:01 pm on Jun 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I do believe the answer in the second post in this thread provided the information you need to get started.

jdMorgan

1:05 pm on Jun 4, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reason you can't pull the query string from the request is likely that the code is looping -- rewriting all requests (including the previously-rewritten ones) to /index.php.

Again, you have to provide a mechanism to detect "this is the first time we're redirecting" mechanism, and this mechanism needs to be based on *something* concrete -- such as the "user not logged in" or "session not yet established for this user" status. So, mod_rewrite needs to check that status indicator before rewriting or redirecting. Above, I proposed using a cookie (likely a pre-existing one, not a new/additional one) to implement this function.

Do you have any specific questions on this method?

Jim