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

Recommended Answers

All 8 Replies

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.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^user/(.*) profile.php?user=$1
</IfModule>

This would then rewrite URLs like http://example.com/user/username to http://example.com/profile.php?user=username

R.

commented: Great help, works a charm! +1

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.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^user/(.*) profile.php?user=$1
</IfModule>

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

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.

Excellent. Cheers, R.

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.

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^user/(.*) profile.php?user=$1
</IfModule>

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.