【问题标题】:Pretty URL Links for Users用户的漂亮 URL 链接
【发布时间】:2016-06-28 18:01:58
【问题描述】:

我正在尝试从 yourwebsite/profile.php?username=funusername 制作我的网址 yourwebsite/funusername。我试过 .htaccess 之类的:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^/([^/]+)$ profile.php?username=$1

(我也有代码可以将.php 从我的页面中删除)。我对 .htaccess 非常缺乏经验,因此非常感谢任何帮助。

奖励:如何在链接中引用此 URL?如<a href="profileurl">

【问题讨论】:

  • 我在整个互联网上搜索了一个解决方案,但要么没有任何东西对我有用,要么我找不到完整的代码示例。
  • 是的,但我在链接中使用 PHP 变量。
  • 好的,但是为什么一般的 .htacces 代码不起作用?

标签: php


【解决方案1】:

您可以创建一个页面 yourwebsite/funusername/index.html 并使用 jQuery 加载加载 yourwebsite/profile.php?username=funusername。

这是一个工作示例:

你的网站代码/funnyusername/index.html:

<!doctype html>
<html>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.0.0.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
  $("html").load("/path/to/profile.php?username=funnyusername");
});
</script>
</html>

然后去掉你的网站/account.php的html标签。

另外,如果您的网站/account.php 中有 &lt;!doctype html&gt; 标记,也将其删除。

例如:

<head>
** Whatewer code you have here **
</head>
<body>
** Whatever code you have here **
</body>

你也可以用 iframe 来做,但我就是这样做的

【讨论】:

  • 我现在正在使用移动设备,所以我不能,但我会尽快这样做。
  • 非常感谢,非常感谢。我觉得我的网站类型有必要让用户名看起来像页面的名称。
  • 抱歉拖了这么久,这里是示例代码。
猜你喜欢
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2014-05-11
  • 2011-07-23
  • 2012-11-01
  • 1970-01-01
  • 2012-01-19
  • 2010-12-05
相关资源
最近更新 更多