我正在创建一个应用程序,您可以将文件存储在文件夹中等等。
我的.htaccess文件中有一点问题,因为我希望能够将两个规则重定向到同一页面,其中一个规则没有。
我希望example.com/drive
将自己重新命名为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]
此代码的工作原理是在网址中使用dir而不是驱动器,但这不是我想要的。
提前致谢
答案 0 :(得分:1)
您的规则是正确的(如果您将dir
更改为drive
,如上所述),您只需稍微重新排序:
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]