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 Answers 3

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
Sign up to request clarification or add additional context in comments.

9 Comments

You probably want to add RewriteCond %{REQUEST_FILENAME} !-d as well, so directories aren't rewritten as well.
@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
@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.
@RobHardgood As Jonah Bron said, at this point they're practically the same. Both will get the job done.
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?
|
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

2 Comments

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).
Oh yeah. I'm new at url rewriting :)
0

You probably want Apache's mod_rewrite.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.