清理localhost上的URL

时间:2014-05-05 10:01:07

标签: php apache .htaccess mod-rewrite clean-urls

我正在尝试在我的localhost上实现干净的URL(所以当我编写localhost / contact时,它会将我重定向到localhost / contact.php),但它无效。我是根据我在YouTube上找到的较旧的教程做到的,但我可能不太了解某些部分。非常感谢每一个答案。这是我的代码:

.htaccess //我确定我允许使用mod_rewrite。

<IfModule mod_rewrite.so>
    RewriteEngine On
    RewriteBase /www/

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php/$1
</IfModule>

index.php中的脚本

 include_once('classes/simpleUrl.php');
 $url = new simpleUrl('localhost');
 $test = $url->segment(1);
if (!$url->segment(1)) {
    $page = 'home';
}

else {  
    $page = $url->segment(1);
}

switch ($page) {
    case 'home' :
            echo 'Home page';
            break;
    case 'contact':
        echo 'Contacts page';
        break;
    default:
        echo '404';
        break;  
}

类/ simpleUrl.php

class simpleUrl {
    var $site_path;



    function __toString() {
        return $this->site_path;
    }

    private function removeSlash($string) {
        if ($string[strlen($string) - 1] == '/') {
            $string = rtrim($string, '/');

        return $string;
        }
    }

    function __construct($site_path) {
        $this->site_path = $this->removeSlash($site_path);
    }

    function segment($segment) {
        $url = str_replace($this->site_path, '', $_SERVER['REQUEST_URI']);
        $url = explode('/', $url);
        if(isset($url[$segment])) {return $url[$segment];} else {return false;}
        return $url;
    }

2 个答案:

答案 0 :(得分:0)

$url = new simpleUrl('localhost'); localhost中未显示$_SERVER['REQUEST_URI'],因此在这种情况下,您只需使用$url = new simpleUrl('index.php');

将此代码用于.htaccess

RewriteEngine On
RewriteBase /www/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^.*$ index.php [L]  

因为当你这样做时:

RewriteRule ^(.*)$ index.php/$1

您的实际网址可能会是这样的:

localhost/index.php/contact

因此,请求更好的网址保持不变。

但在这种情况下使用:

$url = new simpleUrl('');


Oho,看看这个错误:

private function removeSlash($string) {
    if ($string[strlen($string) - 1] == '/') {
        $string = rtrim($string, '/');

    return $string;
    }
}

当传递$string并不以斜杠结束时它不返回任何内容,因此必须更改它:

private function removeSlash($string) {
    if ($string[strlen($string) - 1] == '/') {
        $string = rtrim($string, '/');
    }
    return $string;
}

现在,我将解释何时以及为何使用$site_path simpleUrl构造函数: 如果您在子目录中运行脚本,例如:如果您在/ www / test /下工作,那么您应该test作为参数传递给simpleUrl构造函数,否则将其留空


更新3

您应该将simpleUrl segment方法更改为:

function segment($segment) {
    $url = trim(str_replace($this->site_path, '', $_SERVER['REQUEST_URI']),'/');
    $url = explode('/', $url);
    if(isset($url[$segment])) {return $url[$segment];} else {return false;}
    return $url;
}

然后使用0作为索引,而不是1 - $url->segment(0)

答案 1 :(得分:0)

我不确定您是否希望使用更多变量,例如:www.domain.com/folder-with-website/products/product-name,或者您是否可以使用:www.domain.com/folder -with-网站/产品?ID = 123

但试试这个:

<强>的index.php

<?php
   $page = $_GET["page"];
   if(file_exists("pages/{$page}.php")) {
       include("pages/{$page}.php");
   } else if(empty($page)) {
       include("pages/home.php");
   } else {
       echo "This page does not exists";
   }
?>

<强>的.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ index.php?page=$1

Options -Indexes

链接设置

<ul>
    <li>
        <a href="Home">Home</a> <!-- Just a page without any variables -->
    </li><li>
        <a href="Products">Products</a> <!-- same as Home -->
    </li><li>
        <a href="Details?id=123">Details about product</a> <!-- Page with a variable / extra information -->
    </li><li>
        <a href="Details?id=123&title=hey">Details about product</a> <!-- Page with more variables -->
    </li>
</ul>