2013-04-08 48 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