Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何获得一个类似twitter的url_Url_.htaccess - Fatal编程技术网

如何获得一个类似twitter的url

如何获得一个类似twitter的url,url,.htaccess,Url,.htaccess,我想要一个简单的帮助。。。 我有这样一个url: example.com/profile.php?id=&name= 我的.htaccess文件如下 重写规则^profile/(.)/(.)profile.php?id=$1&name=$2 所以我有一个这样的结束url: example.com/profile/id/name 我可以 example.com/id 但我如何才能获得这样的url: example.com/name ? thax显然,您的profile.php脚本需要两个GET变量

我想要一个简单的帮助。。。 我有这样一个url: example.com/profile.php?id=&name=

我的.htaccess文件如下

重写规则^profile/(.)/(.)profile.php?id=$1&name=$2

所以我有一个这样的结束url: example.com/profile/id/name

我可以 example.com/id

但我如何才能获得这样的url:

example.com/name

?


thax

显然,您的profile.php脚本需要两个GET变量,而您想要的URL只有一个。因此,您可能必须同时更改脚本和数据库模式。

您的重写规则有点错误。您将仅在每个括号内的部分中选择一个字符。如果你在每个点后面加*的话,它会选择一个或多个字符,我认为这是你需要的

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

如果你要找的正是这个:

example.com/name

您需要将profile.php更改为仅使用name变量,并使用它查询数据库

我相信之前你有过这样的经历:

mysql_query("SELECT * from table where id=$id");
您需要将其更改为

mysql_query("SELECT * from table where name$name");
因此,您告诉页面按名称而不是ID查询用户

与此相关的还有一些缺点,因为您的查询不会像以前那样快,因为我相信您的名称列不是主键,因此没有索引

Twitter使用Rails,因此他们将使用类似(onMissingMethod)的方式以稍微不同的方式调用它:

这也不是很好,因为它仍然通过字符串查询数据库,但有一些性能改进,使rails能够做到这一点

您的htaccess将看起来像:

RewriteRule ^(.*) profile.php?name=$1

希望回答您的问题

您的意思是希望您的URL只包含名称,而不包含ID吗?这是正确的,但不能完全回答问题。他正在寻找一个只有1个正斜杠的路径。我得到了一个错误的重定向页面。我可以在url-->profile.php中使用rewritecond?id=$1&name=$2??php确实需要“id”。。。谢谢
RewriteRule ^(.*) profile.php?name=$1