Mod_rewrite
Ahhhh!
OK, so I've been learning PHP for about 7 years now on and off, but never have I taken the time to look into URL rewriting. Until now I've not needed it, but you can guarantee the one thing you think you wont need, you need!
I have the following URL style:
www.sitename.com/profile.php?user=username
Of which I would like rewriting to:
www.sitename.com/username (Preferred)
OR
www.sitename.com/user/username
I also need it to be reversible, so if someone types in www.sitename.com/username it works.
Any ideas?
Previous attempt was setting up mod_rewrite rules in .htaccess, is this the best way?
If not, how?
Any help much appreciated!
Cheers!
Jack
scaasiboi
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 3
scaasiboi
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 3
If someone types: http://example.com/profile.php?user=username , it will not be rewritten, but would instead just call the profile.php script.
You could have it rewrite to: http://example.com/user/username , however Apache would then just need to rewrite it backward, so there isn't really any point. Or am I miss understanding your reasoning?
R.
No your not mis-understanding, I'm being confusing, thanks for your post, I just read back on it and understood your logic!
Cheers,
Jack.
scaasiboi
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 3
twiss
Veteran Poster
1,005 posts since Apr 2010
Reputation Points: 177
Solved Threads: 101
Hi
You could try something like the following in a .htaccess file. To work, this will require you to have the apache rewrite module installed and enabled.
RewriteEngine On
RewriteRule ^user/(.*) profile.php?user=$1
This would then rewrite URLs like http://example.com/user/username to http://example.com/profile.php?user=username
R.
Ahhh!
Ok so it works, kinda...
I seem to have an unformatted pages, do you know why that may be?
If you want to take a look at it live, tell me and i will Direct Message you.
Cheers.
Jack
scaasiboi
Junior Poster in Training
50 posts since Oct 2010
Reputation Points: 10
Solved Threads: 3