Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirects with condition and variable

Wondering if .htaccess can do this

         

jpbaird

2:00 am on Aug 10, 2011 (gmt 0)

10+ Year Member



Hi,

I'm having a problem getting .htaccess to redirect like I want, and after about 6 hours of searching I'm hoping that someone from this forum may be able to help shed some light on this and let me know if it's even possible.

Basically I have a bunch of groups and i want to use sub-directories to redirect people to the same login.php and register.php page, but use that directory as a variable.

So anyone going to www.domain.com/groupname/register.php would see the file located at www.domain.com/register.php, but I want it to still show up as www.domain.com/groupname/register.php.

The second part is that i want to be able to access the "groupname" variable.

Not sure if i explained that very well, but if anyone has an idea of how to accomplish this it would be very much appreciated.

Thanks!

lucy24

2:39 am on Aug 10, 2011 (gmt 0)

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



but I want it to still show up as

In htaccess-speak, you don't want a redirect, you want a rewrite.

The basic pattern is /groupname/register.php >> rewrite >> /register.php

What do you mean by "access"? Are you trying to hold on to the groupname so after they've done the register or login, they get sent to the group they originally asked for? Can't be done with htaccess alone. You need either a cookie or some server-side equivalent. The .htaccess knows where you came from (sort of) but it doesn't know the second-to-last place you were. Unless you start performing hanky-panky with query strings, and you don't want to go there.

jpbaird

2:55 am on Aug 10, 2011 (gmt 0)

10+ Year Member



Thanks for the reply.

Once the user is logged in there is a session variable set with the groupname, but basically I just want anyone going to www.domain.com/"groupname"/"whatever".php to access the root of the website. Mostly for user registration and login. If i can access the "groupname" then I can set the session variable and keep track of the group from there.

lucy24

6:42 am on Aug 10, 2011 (gmt 0)

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



So long as they're on the login/register page, the /groupname/ should still exist. That's the beauty of rewriting instead of redirecting. I don't speak php, but I assume there's a command that gives your current page. The /groupname/ should be in there, so you just need to grab it before they leave the page. When they click the Login button, say.

Then again, you could set your variable when they first arrive on the site, or when they request a particular /groupname/, and just update the variable to its real, grownup form when they actually log in.

jpbaird

5:24 pm on Aug 10, 2011 (gmt 0)

10+ Year Member



Thanks for the help. I think I'll see about getting this rewrite set up!