htaccess获得漂亮的链接

htaccess获得漂亮的链接

本文介绍了如何使用.htaccess获得漂亮的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ID显示成员的记录(个人资料).

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

所以/profile.php?id=1 显示ID为1的成员的数据.

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

但是这些网址很丑陋,也不是SEO友好的.

But these urls are ugly and not SEO friendly either.

我想将URL更改为 profiles/membername 格式(membername是已存储在数据库中的每个成员的唯一别名).

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

如何通过.htaccess实现此链接结构?

How can I achieve this link structure through .htaccess?

推荐答案

这需要安装apache的mod_rewrite模块.您可以通过以下代码来实现所需的目标:

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>

示例:www.yourwebsite.com/profiles/1234-> www.yourwebsitename.com/profile.php?id=1234如果您想要更多变量,只需修改上面的行,如下所示:

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

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

只需编辑.htaccess文件并添加上述行,请确保在进行任何修改之前为.htaccess创建了备份.

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

这篇关于如何使用.htaccess获得漂亮的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:58