954,587 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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
 

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.

blocblue
Posting Pro in Training
475 posts since Jan 2008
Reputation Points: 142
Solved Threads: 79
 

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.

Thanks dude, how would this work the other way?
So if someone typed in http://example.com/profile.php?user=username it would change to http://example.com/user/username

:)
Cheers
Jack

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.

blocblue
Posting Pro in Training
475 posts since Jan 2008
Reputation Points: 142
Solved Threads: 79
 

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
 

Excellent. Cheers, R.

blocblue
Posting Pro in Training
475 posts since Jan 2008
Reputation Points: 142
Solved Threads: 79
 

Note that it's pretty usual to indeed redirect http://example.com/profile.php?user=username to http://example.com/username or http://example.com/user/username . This is also possible with mod_rewrite. It should be something like:

RewriteEngine On
RewriteRule ^profile\.php\?user=(.*) user/$1 [R=301]
RewriteRule ^user/(.*) profile.php?user=$1
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
 

It might be that requests for your CSS and perhaps other images are being rewritten, particularly if their URLs start with 'user/'.

Try adding the two new lines below.

RewriteEngine On

# Check whether request is for a physical file or directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^profile\.php\?user=(.*) user/$1 [R=301]
RewriteRule ^user/(.*) profile.php?user=$1


R.

blocblue
Posting Pro in Training
475 posts since Jan 2008
Reputation Points: 142
Solved Threads: 79
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: