2013-05-02 148 views
1

我想重寫我的網址,但仍然沒有得到成功。我兩天後搜索了Google,但沒有成功。我粘貼我的.htaccess代碼URL重寫隱藏文件夾名稱從URL

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{HTTP_HOST} localhost [NC] 
RewriteCond %{REQUEST_URI} !^/folder1/ 
RewriteRule ^(.*)$ /folder1/$1 [L] 

這是我的目錄:

localhost/project/index.php 
在此頁面

有兩個按鈕。如果一個按鈕按下,它應該重定向到文件夾1/index.php文件否則,如果兩個按鈕按下那麼它應該重定向文件夾2/index.php文件

localhost/project/folder1/index.php 
localhost/project/folder2/index.php 

Basically I don't want display folder1 or folder2 directory names. 
any idea please thanks 

實際工作中的網址,我想顯示應該是這樣的:
本地主機/項目/

+1

你需要告訴我們你喜歡什麼網址每個文件夾顯示爲。您需要以某種方式區分這兩個文件夾。 – 2013-05-02 07:24:04

+0

執行url我想要顯示應該是這樣的: localhost/project/ – Raheel 2013-05-02 07:33:31

+1

好的,文件名? localhost/project/folder1和localhost/project/folder2還是什麼?你不能讓localhost/project參考兩個不同的位置。 – 2013-05-02 07:38:58

回答

-1

我假設你想要的是瀏覽器請求/index.php,但服務器實際使用位於/folder1/index.php的文件,對不對?如果是這樣,這應該工作:

確保apache mod_rewrite模塊已安裝。然後,用在你的Apache配置是這樣的,虛擬主機配置,或.htaccess文件:

RewriteEngine On 
RewriteRule ^/(.*)$ /folder1/$1 

規則使用正則表達式,所以你可能想看看關於這一主題的參考,如果你不確定。閱讀手冊以獲取關於其他指令(RewriteCond可能非常有用)或規則選項的更多信息。

+1

我已經在我的代碼中使用此規則,您可以看到最後一行的規則。 – Raheel 2013-05-02 07:36:08

0

我不相信你能夠單獨使用mod_rewrite來做你正在請求的東西。

的問題是你想1個文件改寫爲2個獨立的位置:

/project/index.php -> /project/folder1/index.php 
/project/index.php -> /project/folder2/index.php 

有了這個邏輯,mod_rewrite的不知道是什麼將客戶點擊,文件夾1或文件夾2。

爲了實現您要查找的內容,您需要在客戶端上設置一個cookie,指示他選擇了「文件夾」 - 然後使用PHP確定最佳操作。這是一個給你一個想法的例子。此代碼不是生產準備好的。

/project/index.php:

$cookie_folder = $_COOKIE['folder']; 
if($cookie_folder == "folder1")  { $folder = 'folder1'; } 
elseif($cookie_folder == "folder2") { $folder = 'folder2'; } 
require_once('/absolute/path/to/project/' . $folder . '/index.php');