【问题标题】:Clean URL on localhost本地主机上的清理 URL
【发布时间】:2014-05-05 10:01:06
【问题描述】:

我试图在我的 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;
    }

【问题讨论】:

  • 详细解释“不工作”的含义会很有帮助。您是否可以显示错误/警告?
  • 另外,从我在您的示例代码中看到的,您似乎正在打印“主页”、“联系人页面”,而不是实际重定向到 contact.php。这是您正在运行的实际代码吗?
  • 是的,这个回显是临时解决方案,看看它是否有效,稍后我将使用包含功能。当我键入 localhost/url 时,它给了我基本错误“/url”未在此服务器上找到。

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


【解决方案1】:

$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 保持不变。

但在这种情况下使用:

$url = new simpleUrl('');


哦,看看这个谜团:

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

您应该将simpleUrls 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-&gt;segment(0)

【讨论】:

  • 我试过了,但是当我写 localhost/products 时仍然出现错误:在此服务器而不是产品页面上找不到请求的 URL /products。
  • 我已多次更新答案,请再次检查,并告诉我
  • 您确定必须使用此行:RewriteBase /www/?尝试评论它并告诉我是否有任何变化
  • 我删除了它并按照您的说明进行操作,但没有更改。当我使用 $url = new simpleUrl('') 时,它给了我错误:注意:未初始化的字符串偏移量:C:\wamp\www\classes\simpleUrl.php 第 12 行中的 -1
  • 我已经更新:removeSlash 方法有错误,你也不需要我上面说的RewriteBase /www/,我已经测试过代码并且它正在工作。
【解决方案2】:

我不确定您是否希望使用更多变量,例如:www.domain.com/folder-with-website/products/product-name 或者您是否可以使用:www.domain.com/folder-with -website/products?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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多