博士重写动态页面


Php re writing dynamic pages

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

我使用的是MySQL数据库。如有任何帮助,我们将不胜感激。

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

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/

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

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

所以你想要site.com/index.php?q=某个/其他/路径在用户浏览器中显示如下:site.com/some/other/path

以下是你的.htaccess文件需要包含的内容(取自FlightPath.htacccess文件,该文件深受Drupal6.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"]的伪路径,并查询数据库并显示您认为合适的内容。

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