Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
htaccessurl创建和php_Php_.htaccess - Fatal编程技术网

htaccessurl创建和php

htaccessurl创建和php,php,.htaccess,Php,.htaccess,人 我的代码有问题 问题是,我的php文件获取了$\u GET值,如下所示: // Look if there is a page asked if(isset( $_GET['section'] )) { // Page asked lets see what we do with it if(isset( $_GET['subtopic'])) { $path = "content/" . $_GET['section'] . "/" . $_GET['sub

我的代码有问题

问题是,我的php文件获取了$\u GET值,如下所示:

// Look if there is a page asked
if(isset( $_GET['section'] )) {
    // Page asked lets see what we do with it
    if(isset( $_GET['subtopic'])) {
        $path = "content/" . $_GET['section'] . "/" . $_GET['subtopic'] . ".php";
        if(file_exists($path)) {
            // Asked ?section=&subtopic= lets check if exist then include
            include($path);
        }
        else {
            // Asked ?section=&subtopic= not found throw 404
            echo '404 - not found';
        }
    }
    else {
        $path = "content/" . $_GET['section'] . "/index.php";
        if(file_exists($path)) {
            // Asked ?section= existed include
            include($path);
        }
        else {
            // Asked ?section not found throw 404
            echo '404 - not found';
        }   
    }
}
else{
    // No page asked throw default page
    include('content/home/news.php');
}
因此,当我有我的ip时,它就像这个ip/?section=home&subtopic=news

它所代表的是该部分是一个文件夹,它应该像上面的代码所示。但我使用.htaccess文件制作?section=home使其成为IP/home/?subtopic=news,但当我制作IP/home/?subtopic=news时,它会给我/home/not found,我认为问题出在我的.htaccess文件中:

RewriteEngine on
RewriteBase /

RewriteRule ^(\W+)/$ /index.php?section=$1
RewriteRule ^(\w+)/(\w+)/$ /index.php?section=$1&subtopic=$2
这是我的.htaccess文件来完成它,但它只是不想工作,所以我希望你们能帮助我

亲切问候,


Jeffry

好吧,我建议您简化一点,只传递一个变量要比传递两个变量容易,然后您可以处理该变量以提取所需的两个数据。为此,您必须将此代码放在.htaccess文件中

现在,在index.php文件中,$\u GET['data']上有2个数据,您所要做的就是像这样分解字符串

$data = explode("/", $_GET['data']);
$section = $data[0];
$subtopic = $data[1];

然后你可以做所有的测试。。。如果在正则表达式中使用“/”,请不要忘记激活apache服务器上的“重写”模块,最好在之前添加symbol\/

此.htaccess指令正在工作:

RewriteEngine on
RewriteBase /
RewriteRule ^(\w+)\/$ /index.php?section=$1
RewriteRule ^(\w+)\/(\w+)\/$ /index.php?section=$1&subtopic=$2
还需要启用apache.htaccess文件。 您需要修改包含AllowOverride None的行以读取apache配置或virtualhost文件中的AllowOverride All。这告诉Apache允许.htaccess文件超越以前的指令是可以的。 例如:

必须重新加载Apache,此更改才会生效

RewriteEngine on
RewriteBase /
RewriteRule ^(\w+)\/$ /index.php?section=$1
RewriteRule ^(\w+)\/(\w+)\/$ /index.php?section=$1&subtopic=$2
    <Directory /var/www/> # /var/www/ - web site root directory
            Options Indexes FollowSymLinks MultiViews
            AllowOverride All
            Order allow,deny
            allow from all
    </Directory>