views:

48

answers:

3

I think I can use the .htaccess file for this, but I've looked it up and not found anything useful. What I want to do is have my site redirect to a php page when a user types their username in the URL like:

example.com/username

And have it be the same as a PHP page like:

example.com/name.php?id=username

I'd like it to display as example.com/username even after it redirects, but it is not necessary. Any ideas?

A: 

You probably want Apache's mod_rewrite.

Gian
+1  A: 

You can use mod_rewrite to transparently rewrite your URLs on the server.

Assuming that you'd only have usernames following your domain, something like this would do what you want:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .+ name.php?id=$0
Tim Stone
You probably want to add `RewriteCond %{REQUEST_FILENAME} !-d` as well, so directories aren't rewritten as well.
vonconrad
@vonconrad you seem to know what you're talking about, and i'm completely new to editing .htaccess files... Which of these examples do you think would work better? Thanks for your help
RobHardgood
@vonconrad - Yeah, I had excluded it for brevity, but without knowing the OP's scenario, it's probably safer to include it. I'll amend the ruleset.
Tim Stone
@RobHardgood at this point, either will do fine ;)
Jonah Bron
@RobHardgood As Jonah Bron said, at this point they're practically the same. Both will get the job done.
vonconrad
I see they're looking pretty much teh same now, but in the last line i see a couple differences... [L] and (.+)/.+ and $1/$0can someone explain what those differences mean?
RobHardgood
@RobHardgood - The test pattern will automatically capture the entire matched input in the backreference `$0`, whereas the `$1` backreference comes from the first capture (indicated by the parenthesis). The `L` flag indicates that this is the last rule to be executed for this ruleset (although it's a bit more complicated, see [my answer to this question](http://stackoverflow.com/questions/3482106/rewrite-problem-last-not-being-respected/3482267#3482267)), which is important if you have rules later on that would unintentionally match the result of this rewrite.
Tim Stone
Ah, i see. One more thing, how would i write it when name.php is in another directory? Like example.com/names/name.phpThanks for everyone's help :D
RobHardgood
@RobHardgood - If you want `/names/` in the URL, you can either drop `.htaccess` in the `/names` directory, or do `RewriteRule ^names/(.+)$ names/name.php?id=$1`. If not, do `RewriteRule .+ names/name.php?id=$0`
Tim Stone
Ah, that's very simple. Thanks @Tim for your help
RobHardgood
+1  A: 
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) users.php?user=$1 [L]

I think that will work.

The Apache mod_rewrite guide is here http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

Jonah Bron
You don't need the tilde, since you're already instructing mod_rewrite to only apply the rule if the request isn't a real directory or file (see the two RewriteCond just above the RewriteRule).
vonconrad
Oh yeah. I'm new at url rewriting :)
Jonah Bron