-2

Possible Duplicate:
.htacces to create friendly URLs. Help needed

I am using ids to display the records (profiles ) of members.

So /profile.php?id=1 displays the data of member with id 1.

But these urls are ugly and not SEO friendly either.

I want change the urls to the format profiles/membername (membername is a unique alias for each member already stored in the db).

How can I achieve this link structure through .htaccess?

Community
  • 1
  • 1
Ahmar Ali
  • 541
  • 2
  • 7
  • 21

2 Answers2

2

This requires apache's mod_rewrite module to be installed. What you want can be achieved through the following piece of code:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^profiles/(.*)?$ profile.php?id=$1
</IfModule>

Example: www.yourwebsite.com/profiles/1234 -> www.yourwebsitename.com/profile.php?id=1234 If you want more variables just modify the above line like below:

RewriteRule ^profiles/(.*)/(.*)?$ profile.php?id=$1&var2=$2

Just edit your .htaccess file and add the above lines, make sure you create a backup of your .htaccess before making any modifications.

  • Thanks Sathiya but what if there are thousands of potential id's so do I have to create the rule for each of them? – Ahmar Ali Jan 27 '13 at 11:21
  • Not really, the single rule above acts as a wild card entry: RewriteRule ^profiles/(.*)?$ profile.php?id=$1 That means its valid for all URL's of the form /profiles/1234 or /profiles/1235 etc, wherein the '1234' and '1235' are values for the ID variable. The second rule I posted was just an example showing you how to rewrite URL's in case you wanted to send more than one parameter i.e ?id=1234&name=John – Sathiya Sundaram Jan 27 '13 at 11:54
1

Firstly see some articles about problematics http://httpd.apache.org/docs/2.2/howto/htaccess.html

RewriteEngine On

RewriteRule ^profiles/(a-z0-9]+)$ /profile.php?username=$1 [L,QSA]

so e.g. /profiles/membername ---> /profile.php?username=membername

mychalvlcek
  • 3,956
  • 1
  • 19
  • 34