How to use .htaccess for beautiful links [duplicate]

て烟熏妆下的殇ゞ 提交于 2021-02-05 12:28:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!