0

我有一个名为 profile.php?id=21 的页面,我希望从存储在数据库中的文本字符串中选择路径。因此,例如,如果我将“我的页面”存储在与此页面关联的数据库中,我希望完整路径为“www.domainname.com/my-page/21”而不是“www.domainname.com/profile”。 php?id=21”。

我正在使用 MySQL 数据库。任何帮助,将不胜感激。

4

2 回答 2

0

这可能会对您有所帮助,请将其添加到 .htacess 文件中

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /profile.php?id=$2 [L]

然后这些中的任何一个都会调用domainname.com/profile.php?id=21

domainname.com/my-page/21/
domainname.com/anything/21/
domainname.com/justnotaslash/21/
于 2013-07-07T18:26:57.177 回答
0

只是为了扩展上面 Andy Gee 的答案,您需要在网站的基本目录中创建一个 .htaccess 文件。(这就是 Drupal (drupal.org) 和 FlightPath (getflightpath.com) 等项目的做法)。

假设所有流量都需要通过 index.php,并且您计划在 index.php?q=some/path 处有一个伪路径

因此,您希望 site.com/index.php?q=some/other/path 在用户的浏览器中看起来像这样:site.com/some/other/path

这是您的 .htaccess 文件需要包含的内容(取自 FlightPath .htaccess 文件,该文件受 Drupal 6 .htaccess 文件的影响很大):

<IfModule mod_rewrite.c>
  RewriteEngine on

#######################################
######################################  

  # Modify the RewriteBase if you are using FlightPath in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/flightpath uncomment and
  # modify the following line:
  # RewriteBase /flightpath
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

  # Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_URI} !=/favicon.ico
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

在此之后,您需要编写 index.php(或任何您想使用的程序)来查看 $_GET["q"] 的伪路径,并查询数据库并显示您认为合适的内容。

顺便说一句,如果你把这个 .htaccess 文件放在适当的位置,并开始出现奇怪的服务器错误,只需删除或重命名 .htaccess 文件即可恢复正常,因为这可能意味着你的网络服务器没有设置为处理这样​​的 URL 重写规则.

于 2013-07-07T19:26:56.027 回答