Pregunta

I'm trying to achieve clean URLs on my localhost (so when I write localhost/contact, it redirects me to localhost/contact.php), but it's not working. I did it according to the older tutorial I found on YouTube, but I probably didn't understand well some parts. Many thanks for every answer. Here is my code:

.htaccess // I am sure I have mod_rewrite allowed.

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

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

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

Script in 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;  
}

classes/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;
    }
¿Fue útil?

Solución

$url = new simpleUrl('localhost'); localhost is not shown in $_SERVER['REQUEST_URI'] so in this case you may simply use $url = new simpleUrl('index.php');

OR

Use this code for .htaccess :

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

because when you do this:

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

Your actual URL may become something like this:

localhost/index.php/contact

So it's better requested URL to stay same.

but in this case use:

$url = new simpleUrl('');


Oho, look at this mistery:

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

    return $string;
    }
}

when passed $string does not ends with slash it returns nothing, so have to change it in such:

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

Now I'll explain when and why to use $site_path for simpleUrl constructor: if you have running your script in subdirectory, for ex: if you are working under /www/test/ then you should you test as param to pass to simpleUrl constructor, otherwise leave it blank


Update 3

You should change simpleUrls segment method to this:

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;
}

and next use 0 as index instead of 1 - $url->segment(0)

Otros consejos

I'm not sure if you wish to use more variables like: www.domain.com/folder-with-website/products/product-name or if you could live with: www.domain.com/folder-with-website/products?id=123

But try this:

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

Link setup

<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>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top