Mobile app version of vmapp.org
Login or Join
Welton855

: Htaccess redirect pages that don't exist 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

@Welton855

Posted in: #Apache #Htaccess #Php #Redirects #Url

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.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Welton855

1 Comments

Sorted by latest first Latest Oldest Best

 

@Berumen354

Well, if you literally used the following rule:

Redirect 302 /username domain.com/?username


then it probably tried to redirect you to 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 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_-]+)$ domain.com/?

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 domain.com/username it would look like domain.com/u/username. The RedirectMatch-rule for that would look like:

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

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.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme