Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How rewriting for directory in root directory

I want to change my url. Here I have a directory structure like this

htdocs/
      example/
              public/
                     login.php
                     people/
                            people1.php
                            people2.php
                     animal/
                            animal1.php
                            animal2.php
                     404.php
              assets/
                    css/
                    js/

then I want the url like below in accordance with the existing directory in the root

localhost/example/login
localhost/example/people/people1
localhost/example/people/people2
localhost/example/animal/animal1
localhost/example/animal/animal2

I've tried making an .htaccess file with the following contents

Options +FollowSymLinks
RewriteEngine On
rewritecond %{REQUEST_URI} !^/public/(.*)
rewritecond %{REQUEST_URI} !^/assets/(.*)
RewriteRule .* index.php [L]

and it's index.php

$requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI'];

switch ( $requested ) {

    case '/login':
        include 'public/login.php';
        break;
    default:
        include 'public/404.php';
}

when I headed localhost/example/login, but destination is 404.php (ERROR).

can you help me?

like image 314
ramadani Avatar asked Dec 18 '13 03:12

ramadani


People also ask

What is the root of a directory?

The root directory is the top-level directory in a folder structure. All of the other folders grow outwards from the root directory, so it makes sense to think of the root directory as the trunk of a tree from which the branches grow.

What is the purpose of a root directory?

Tree-structured directories (sometimes called hierarchical directories) allow users to create directories within directories at will. A master or root directory contains pointers to main subdirectories. Data files can be mentioned in these subdirectories, or further, sub-subdirectories can be created.


1 Answers

The $_SERVER['REQUEST_URI'] variable is the entire URI. So if are going to http://example.com/example/login the $_SERVER['REQUEST_URI'] variable is /example/login. Something that you could try doing is changing your htaccess file to:

Options +FollowSymLinks
RewriteEngine On
rewritecond %{REQUEST_URI} !/public/(.*)
rewritecond %{REQUEST_URI} !/assets/(.*)
RewriteRule ^(.*)$ index.php/$1 [L]

(Note that ^/public/ will never match, because the REQUEST_URI would be /example/public)

Then in your code use $_SERVER['PATH_INFO'] instead.

like image 108
Jon Lin Avatar answered Oct 16 '22 04:10

Jon Lin