2013-04-08 45 views
-1

我正在做一個應用程序,您可以在文件夾中存儲文件等。.htaccess雙重寫規則到同一個文件

我在我的.htaccess文件中有一個小問題,因爲我希望能夠將兩個規則重定向到同一個頁面,其中一個沒有屬性。

我想example.com/drive本身reqrite到example.com/drive.php和它這樣做這部分工作,但我也想example.com/drive/path/to/a/dir/here/本身重寫example.com/drive?dir=drive/path/to/a/dir/here/

這可能與.htaccess文件?如果是,如何?

這是我到目前爲止的代碼:

DirectoryIndex home.php 
DirectoryIndex index.php 

ErrorDocument 404 /err/404.php 
ErrorDocument 500 /err/500.php 

Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
RewriteRule ^(.+)$ /$1.php [L,QSA] 
RewriteRule ^u/(.+)$ /profile.php?u=$1 [L,QSA] 
RewriteRule ^f/(.+)$ /file.php?f=$1 [L,QSA] 
RewriteRule ^s/(.+)/(.+)$ /page.php?p=$1&f=$2 [L,QSA] 
RewriteRule ^s/(.+)$ /page.php?p=$1 [L,QSA] 
RewriteRule ^dir/(.+)$ /drive.php?dir=$1 [L,QSA] 

此代碼使用目錄中的URL不會開車,但是這不是我想要的。

在此先感謝

+0

你覺得爲什麼它的作品使用* dir *而不是* drive *?是* example.com/drive/path/to/a/dir/here/*中的所有文件夾,變量?意思是,它們是動態變化的? – 5ervant 2013-04-08 15:33:20

回答

1

你的規則將是正確的(如果你改變dirdrive如前所述),你只需要重新安排他們一點:

DirectoryIndex home.php 
DirectoryIndex index.php 

ErrorDocument 404 /err/404.php 
ErrorDocument 500 /err/500.php 

Options +FollowSymlinks 
RewriteEngine On 
RewriteBase/

# first handle these explicit pattern matches 
RewriteRule ^u/(.+)$   /profile.php?u=$1 [L,QSA] 
RewriteRule ^f/(.+)$   /file.php?f=$1  [L,QSA] 
# it's quite likely this is what you want here instead: 
RewriteRule ^s/([^/]+)/(.+)$ /page.php?p=$1&f=$2 [L,QSA] 
RewriteRule ^s/(.+)$   /page.php?p=$1  [L,QSA] 
RewriteRule ^drive/(.+)$  /drive.php?dir=$1 [L,QSA] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f 
# I'm guessing you want to only rewrite to script that exists in the root, if 
# this is not the case you can change this back to .+ 
RewriteRule ^[^/]+$ /$0.php [L,QSA] 
+0

非常感謝,現在它完美的工作! :-) – 2013-04-08 17:20:21