问题
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?
回答1:
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.
回答2:
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
来源:https://stackoverflow.com/questions/14546712/how-to-use-htaccess-for-beautiful-links