0

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

3
Contributors
8
Replies
9
Views
6 Years
Discussion Span
Last Post by blocblue
0

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

0

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.

0

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

0

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.