2013-07-07 82 views
0

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

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

+0

所以你只是想要一个不同的URL格式? – crowder

+0

放下21,只需使用'www.domainname.com/my-page''my-page'作为你在db中寻找的slu slu。 –

回答

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/ 
+0

非常感谢!这正是我所期待的。我知道现在看起来很简单!再次感谢。 – davelowe85

0

只是对安迪·吉的回答上面扩展,您需要创建一个.htaccess文件在您的网站的基本目录中。 (这就是Drupal(drupal.org)和FlightPath(getflightpath.com)这样的项目)。

比方说,所有的流量需要经过的index.php,并打算在index.php中有一个准路径?Q =一些/路径

所以,你要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> 

Af在这种情况下,您需要对index.php(或任何您想使用的)进行编程,查看伪路径的$ _GET [“q”],然后查询数据库并显示您认为合适的内容。

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

+0

非常感谢你。我遇到过这种htaccess文件(我认为wordpress的文件类似),但说实话,我并不确定在index.php后如何开始。如果我将来遇到奇怪的服务器错误,还要感谢提示! – davelowe85