Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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
Php 清理URL_Php_Html - Fatal编程技术网

Php 清理URL

Php 清理URL,php,html,Php,Html,我目前有一个url,它看起来像 portfolio/index.php?page=home 我的网站使用简单的PHP通过索引运行所有代码。为了让它工作,我的index.php看起来像这样 然后,我的标题链接执行此操作,以便正确加载页面: 我是否可以编写PHP来返回一个外观更好的URL 而不是index.php 任何关于清理我的代码的建议都将不胜感激,因为我对PHP非常陌生。请将此代码放在index.PHP所在的.htaccess中 Options +FollowSymli

我目前有一个url,它看起来像

portfolio/index.php?page=home

我的网站使用简单的PHP通过索引运行所有代码。为了让它工作,我的index.php看起来像这样


然后,我的标题链接执行此操作,以便正确加载页面:

我是否可以编写PHP来返回一个外观更好的URL

而不是index.php


任何关于清理我的代码的建议都将不胜感激,因为我对PHP非常陌生。

请将此代码放在index.PHP所在的.htaccess中

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
为了进行测试,在index.php中,尝试这样做

<?php
  $page = ( isset($_GET['page']) ) ? $_GET['page'] : 'home';
  echo $page;
?>

在您的page.php中

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <ul class="links">
    <li><a href="http://localhost/test/home">HOME</a></li>
    <li><a href="http://localhost/test/work">WORK</a></li>
    <li><a href="http://localhost/test/get-in-touch">ABOUT</a></li>
    </ul>
  </body>
</html>

这必须是结果,无论是在家、工作还是联系上


希望这会有所帮助。

在index.php所在的位置将此代码放在.htaccess中

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [L]
为了进行测试,在index.php中,尝试这样做

<?php
  $page = ( isset($_GET['page']) ) ? $_GET['page'] : 'home';
  echo $page;
?>

在您的page.php中

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <ul class="links">
    <li><a href="http://localhost/test/home">HOME</a></li>
    <li><a href="http://localhost/test/work">WORK</a></li>
    <li><a href="http://localhost/test/get-in-touch">ABOUT</a></li>
    </ul>
  </body>
</html>

这必须是结果,无论是在家、工作还是联系上


希望这会有所帮助。

要回答仅使用PHP而不重写模块的问题,您只需省略索引页,使用页面名称作为数组索引,然后检查是否已设置,但URL中仍有一个

<div id="wrapper">
<?php 
    // INCLUDING HEADER ON ALL PAGES
    include("pages/header.php");
?>

<main> <!-- MAIN TAG !-->
<?php 
    $pageLoaded = array('home', 'header' , 'get-in-touch' , 'work' , 'main'); // LIST ALL PAGES

    $get_keys = array_keys($_GET); // find all the pages in url, in case of multiple
    $matches = array_intersect($pageLoaded,$get_keys); // find matches

    if(!empty($matches)) $page = $matches[0];
    else $page = 'home';

    if ( in_array($page, $pageLoaded) ){ // now redundant, may want to play with other ways of serving 404
        include("pages/$page.php");
    } 

    else {
        include("pages/404.php");
    }
?>
</main>

<?php 
    // INCLUDING FOOTER ON ALL PAGES
    include("pages/footer.php");
?>

现在,您的标题链接可以如下所示:

<ul class="links">
  <li><a href="?home">HOME</a></li>
  <li><a href="?work">WORK</a></li>
  <li><a href="?get-in-touch">ABOUT</a></li>
</ul>

要回答仅使用PHP而不重写模块的问题,您只需省略索引页面,使用页面名称作为数组索引,然后检查是否已设置,但URL中仍有一个

<div id="wrapper">
<?php 
    // INCLUDING HEADER ON ALL PAGES
    include("pages/header.php");
?>

<main> <!-- MAIN TAG !-->
<?php 
    $pageLoaded = array('home', 'header' , 'get-in-touch' , 'work' , 'main'); // LIST ALL PAGES

    $get_keys = array_keys($_GET); // find all the pages in url, in case of multiple
    $matches = array_intersect($pageLoaded,$get_keys); // find matches

    if(!empty($matches)) $page = $matches[0];
    else $page = 'home';

    if ( in_array($page, $pageLoaded) ){ // now redundant, may want to play with other ways of serving 404
        include("pages/$page.php");
    } 

    else {
        include("pages/404.php");
    }
?>
</main>

<?php 
    // INCLUDING FOOTER ON ALL PAGES
    include("pages/footer.php");
?>

现在,您的标题链接可以如下所示:

<ul class="links">
  <li><a href="?home">HOME</a></li>
  <li><a href="?work">WORK</a></li>
  <li><a href="?get-in-touch">ABOUT</a></li>
</ul>

Google“url rewriting”或“pretty url”至于“clean up my code”请求,您应该检查一下是否需要使用.htaccess。这是一个基本要求,所以你可以通过简单的谷歌搜索找到它。谷歌“url重写”或“漂亮的url”至于“清理我的代码”请求,你应该检查一下你需要使用.htaccess来做这件事。这是一个基本要求,所以你可以通过简单的google search.hmmm找到它,我把它添加到了我的.htaccess中。现在的链接只是沿着“pages/work”(页面/工作)的线条重新指向某个内容,所以它应该是“portfolio/pages/work”(组合/页面/工作),它会删除该部分。在这种情况下,您必须创建一个routerhmmm,我将其添加到我的.htaccess中。此时的链接只是沿着“pages/work”这一行重新指向某个内容,因此它应该位于“portfolio/pages/work”的位置,它会删除该部分。在这种情况下,您必须创建一个路由器