【问题标题】:How can I replace ?p=page to /page? [duplicate]如何将 ?p=page 替换为 /page? [复制]
【发布时间】:2017-05-17 05:39:30
【问题描述】:

我试图在其他网站上查找是否有办法在我的 PHP 页面中将 ?p=page 替换为 /page。我想从http://localhost/tapromtour.com/?p=tour_discovery 更改为http://localhost/tapromtour.com/tour_discovery。我主页中的代码如下:

<?php
    if (isset($_GET["p"])){
        $ext =".php";
        $file_name = $_GET["p"];
        $file = "view/".$file_name.$ext;
        if (file_exists($file)){
        include $file;
        }else{
        echo 'no file found';
        }
    }else{
        echo '<meta http-equiv="refresh" content="0; url=?p=home">';
    }
?>

我使用此代码从一个页面链接到另一个页面。有什么方法可以从"localhost/tapromtour.com/?p=tour_discovery" 更改为"localhost/tapromtour.com/tour_discovery"? 如果有请帮助告诉我该怎么做。谢谢

http://localhost/tapromtour.com/?p=tour_discovery http://localhost/tapromtour.com/tour_discovery

【问题讨论】:

  • 使用.htaccess
  • 你能告诉我如何使用它吗?
  • google .htaccess -> 尝试 -> 失败 -> 询问失败
  • 我知道你想做什么,但你的方法不对。

标签: php get web


【解决方案1】:

现在我按照 Abhishek gurjar 的回答做了。这是我现在 index.php 中的代码:

<?php
    if (isset($_GET["p"])){
        $ext =".php";
        $file_name = $_GET["p"];
        $file = "view/".$file_name.$ext;
        if (file_exists($file)){
        include $file;
        }else{
        echo 'no file found';
        }
    }else{
        echo '<meta http-equiv="refresh" content="0; url=home">';
    }
?>

我从echo '&lt;meta http-equiv="refresh" content="0; url=?p=home"&gt;'; 更改为echo '&lt;meta http-equiv="refresh" content="0; url=home"&gt;';,所以它工作得很好。

【讨论】:

  • 没错。当我在 OP 下发帖时,甚至没有意识到这是在这里
【解决方案2】:

您可以在根目录或拥有项目文件的目录中使用此.htaccess 规则,我假设您正在获取index.php 文件中的数据。

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?p=$1 [QSA,L]

创建一个 txt 文件,将其命名为 .htaccess 而不是 htaccess.txt 将此规则放入其中,然后尝试您的网址 http://localhost/tapromtour.com/tour_discovery

【讨论】:

  • 谢谢。这真的很有帮助。
  • 如果认为有帮助,您可以通过接受它来确认这是一个答案,这样对未来的访问者会更有帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-11
  • 1970-01-01
  • 2015-08-19
  • 1970-01-01
  • 2012-10-18
相关资源
最近更新 更多