2013-12-18 51 views
6

我想更改我的網址。在這裏,我有這樣的如何在根目錄中重寫目錄

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

的目錄結構,那麼我想要的網址,如下所示按照在根與現有的目錄

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

我試圖做一個的.htaccess文件用以下內容

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

,它的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'; 
} 

當我往localhost/example /登錄,但目標是404.php(錯誤)。

你能幫助我嗎?

+0

所以你有一些'rewritecond' ...'rewriterule'在哪裏? –

+0

@AndyJones你能再次檢查我的問題嗎? – ramadani

+1

你試過http:// localhost/example/login?或者是其他東西?你是否在你的404.php頁面中的某處轉儲了$ _SERVER [「REQUEST_URI」]? –

回答

1

$_SERVER['REQUEST_URI']變量是整個 URI。所以如果要去http://example.com/example/login$_SERVER['REQUEST_URI']變量是/example/login。的東西,你可以嘗試做的是改變你的htaccess文件:

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

(請注意,^/public/不會匹配,因爲REQUEST_URI/example/public

然後在你的代碼中使用$_SERVER['PATH_INFO']代替。

1

貌似你試圖隱藏PHP擴展的優先級設置爲:

  1. 公共目錄
  2. 別處

將這個代碼在你DOCUMENT_ROOT/.htaccess文件:

RewriteEngine On 

# skip for existing files/directories (/assets will be skipped here) 
RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule^- [L] 

# try to load PHP file from public directory 
RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f 
RewriteRule ^(.+?)/?$ /public/$1.php [L] 

# now try .php elsewhere 
RewriteCond %{DOCUMENT_ROOT}/$1.php -f 
RewriteRule ^(.+?)/?$ /$1.php [L]