1

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?

3 Answers3

2

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
  • 19,119
  • 6
  • 56
  • 66
  • 1
    You probably want to add `RewriteCond %{REQUEST_FILENAME} !-d` as well, so directories aren't rewritten as well. – vonconrad Aug 18 '10 at 01:10
  • @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 Aug 18 '10 at 01:12
  • @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 Aug 18 '10 at 01:14
  • @RobHardgood As Jonah Bron said, at this point they're practically the same. Both will get the job done. – vonconrad Aug 18 '10 at 01:18
  • I see they're looking pretty much teh same now, but in the last line i see a couple differences... [L] and (.+)/.+ and $1/$0 can someone explain what those differences mean? – RobHardgood Aug 18 '10 at 01:31
  • @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 Aug 18 '10 at 01:43
  • Ah, i see. One more thing, how would i write it when name.php is in another directory? Like example.com/names/name.php Thanks for everyone's help :D – RobHardgood Aug 18 '10 at 02:02
  • @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 Aug 18 '10 at 02:13
  • Ah, that's very simple. Thanks @Tim for your help – RobHardgood Aug 19 '10 at 21:14
2
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
  • 9,991
  • 5
  • 45
  • 79
  • 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 Aug 18 '10 at 01:09
  • Oh yeah. I'm new at url rewriting :) – Jonah Aug 18 '10 at 01:10
0

You probably want Apache's mod_rewrite.

Gian
  • 13,735
  • 44
  • 51