删除子文件夹并使用htaccess在url中获取pageName

时间:2016-03-02 03:58:06

标签: php apache .htaccess mod-rewrite

我想从网址中删除子文件夹,并使用.htaccess从网址获取网页名称。

假设我的真实目录结构是:

 /public_html/promoted-content/gui/promoted-content.php
 /public_html/promoted-content/pages.php

Pages.php

 switch($_GET['pageName']) {

    case 'home': {
        include "gui/promoted-content.php";
        break;
    }

    case 'promoted-content-news': {
        $default_title = "News | ".$default_title;
        include "gui/promoted-content.php";
        break;
    }


    case 'promoted-content-news-details': {
        $default_title = "News | ".$default_title;
        include "gui/promoted_contentdetail.php";
        break;
    }

的.htaccess

  RewriteRule ^([A-Za-z0-9-]+)/?$ promoted-content/index.php?pageName=pages&id=$1    [NC,L]    # Process Pagename requests

所以,我希望将网址www.example.com/promoted-content/gui/promoted-content.php?id=1重新命名为:

 www.example.com/promoted-content-news/pageid/pagetitle

1 个答案:

答案 0 :(得分:0)

你需要在.htaccess中捕获3个参数,一个用于pageName,一个用于id,一个用于标题:

#make sure existing files and directories work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([0-9]+)/(.*)$ promoted-content/index.php?pageName=$1&id=$2&title=$3 [L]