我有一个名为profile.php?id=21的页面,我希望从存储在数据库中的文本字符串中选择路径。因此,例如,如果我将"my- page“存储在与此页面关联的数据库中,我希望完整路径为"www.domainname.com/my-page/21”,而不是"www.domainname.com/profile.php?id=21“。
我使用的是MySQL数据库。任何帮助都将不胜感激。
发布于 2013-07-08 02:26:57
这可能会对您有所帮助,请将其添加到.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-08 03:26:56
只是为了扩展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文件,该文件受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“,并查询数据库并以您认为合适的方式显示内容。
顺便说一句,如果你把这个.htaccess文件放在适当的位置,并开始得到奇怪的服务器错误,只要删除或重命名.htaccess文件就可以恢复正常,因为这可能意味着你的set服务器没有设置为处理像这样的.htaccess重写规则。
https://stackoverflow.com/questions/17514914
复制相似问题