Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

I have a website that has a little over 100 members. Each member gets a replicated website, and by default the software we use sets up their replicated website at a url like:

domain.com/?username

"username" being the username they registered with. What I'm wanting to do is allow the users to access their replicated website without having to type the question mark before their username. For example, I would want domain.com/username to redirect to domain.com/?username.

I tried adding the following to my htaccess file:

Redirect 302 /username domain.com/?username

This doesn't work, it just gives a 404 not found page when trying to access domain.com/username because it doesn't actually exist.

Basically, I just want my users to be able to access their websites without having to type the question mark. I hope I've made what I'm trying to accomplish clear.

Is this possible?

Any help is greatly appreciated.

Thanks.

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

Well, if you literally used the following rule:

Redirect 302 /username domain.com/?username

then it probably tried to redirect you to http://domain.com/domain.com/?username - because you didn't provide an absolute path (with my Apache 2.4.6 this rule even returns an internal server error). The following should work:

Redirect 302 /username http://domain.com/?username

But this only works for username, so if you don't want to add this rule for every user in your database, you could make use of the RedirectMatch statement:

RedirectMatch 302 /([a-zA-Z0-9_\-]+)$ http://domain.com/?$1

As you see, I capture the username with the pattern ([a-zA-Z0-9_\-]+) - so if your usernames can contain other characters, you'll have to modify that pattern. But beware: with this method your username-patterns must not contain dots. The reason being, that this rule would then match for "normal" filenames like index.php or header.png, which would break your site.

You could solve that by prepending a virtual directory like /u/ before the username. Instead of http://domain.com/username it would look like http://domain.com/u/username. The RedirectMatch-rule for that would look like:

RedirectMatch 302 /u/([a-zA-Z0-9_\-\.]+)$ http://domain.com/?$1

You still want to limit the characters in your pattern because an username like username&admin=1 sets two $_GET-elements: "username" => "" and "admin" => "1" - which is probably not what you would be expecting.

There are other solutions using mod_rewrite which might be cleaner (some of them would require you to change your PHP-scripts). If you can use mod_rewrite make a comment and I'll add some examples if you like.

share|improve this answer
    
Fantastic! Your solution works perfectly and I appreciate the long and detailed answer. Thanks a ton! –  Thomas Spade Sep 5 '13 at 18:36
    
+1 Nicely detailed answer! –  dan Sep 6 '13 at 8:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.