【问题标题】:Changing a webpage to a 'directory'将网页更改为“目录”
【发布时间】:2014-03-29 13:39:30
【问题描述】:

我不知道你怎么称呼它,但谁能帮我解决这个问题: 有没有办法将 .php 文件设置为目录。

示例: 我在同一目录中有 2 个文件:onepage.php 和 secondpage.php。如果我想访问 index.php,我会去 ..../onepage.php(比如说:www.stackoverflow.com/onepage.php)。然后,如果我想访问第二页,我会去 www.stackoverflow.com/secondpage.php。但是,有没有办法让它显示为 www.stackoverflow.com/secondpage?

另外,如果我举同样的例子,假设我的 secondpage.php 中有一个 $_GET,还有没有办法做到 www.stackoverflow.com/secondpage/($_GET value) ?

谢谢。

【问题讨论】:

  • 快速获胜是在你的根目录下添加一个文件夹,称为 secondpage 然后添加一个 index.php 文件,添加代码以捕获获取请求

标签: php html directory


【解决方案1】:

这可以通过为您的 Apache 服务器使用 mod_rewrite 并使用 .htaccess 设置规则以有效地重写可见 URL 来实现。

【讨论】:

    【解决方案2】:

    .htaccess 文件中使用mod_rewrite。像这样的代码:

    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule secondpage/(.*)$ index.php?var1=$1 [L]
    

    现在$_GET['var1'] 是您的变量。 祝你好运。

    【讨论】:

      【解决方案3】:

      这是从我的网站复制的工作代码

      创建一个名为 .htaccess 的文件

      添加此代码:

      RewriteEngine On
      
      # Run everything else but real files through parse.php
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_URI} !\.(css|png|html|js|json|jpg|jpeg)$
      RewriteRule ^(.*)$ parse.php?info=$1 [L]
      

      现在在你的 parse.php 文件中有这个代码:

      <?php
      
      $getVars = $_GET['info'];
      $vars = explode("/",$getVars);
      
      if(count($vars)<1){
       $vars[0] = "home";
       $vars[1] = "index";
      }else if(count($vars)<2){
       $vars[1] = "index";
      }
      
      
       $fileString = $vars[0] . '/' . $vars[1] . '.php';
      if(file_exists($fileString)){
        include_once($fileString);
      }else{
        include_once("error/404.php");
      }
      

      现在添加如下文件:

      目录:我的目录 文件:index.php

      以 myurl.com/mydirectory/ 访问

      【讨论】:

        猜你喜欢
        • 2011-09-28
        • 1970-01-01
        • 2011-08-24
        • 2015-03-09
        • 2014-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多