我需要在调用虚拟目录时检查文件,并根据页面是否存在重写
示例:
/test/page-one - The page /page-one.php exist, so it would call page-one.php?test=1
/test/test-one - The page /test-one.php does not exist, so it would call index.php?test=1&page=test-one
当前.htaccess一切正常
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ pages/$3.php?page=$1&sub_page=$2 [NC,L,QSA]
RewriteCond %{DOCUMENT_ROOT}/pages/$2.php -f
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ pages/$2.php?sub_page=$1 [NC,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.php?page=$1&sub_page=$2 [NC,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?sub_page=$1 [NC,L,QSA]
所以,我需要检查/ test /是否是第一个目录并执行当前.htaccess所做的操作,但是在重写中添加?test = 1
答案 0 :(得分:0)
您可以在站点根目录中使用此代码.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
## one level virtual directory with /test/
RewriteRule ^(test)/?$ index.php?$1=1 [L,NC,QSA]
## two level virtual directory with /test/
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(test)/([\w-]+)/?$ $2.php?$1=1 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/pages/$2.php -f
RewriteRule ^(test)/([\w-]+)/?$ pages/$2.php?$1=1 [L,NC,QSA]
RewriteRule ^(test)/([\w-]+)/?$ index.php?$1=1&page=$2 [L,NC,QSA]
## three level virtual directory with /test/
RewriteCond %{DOCUMENT_ROOT}/$2.php -f
RewriteRule ^(test)/([\w-]+)/([\w-]+)/?$ $2.php?sub_page=$3&$1=1 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/pages/$2.php -f
RewriteRule ^(test)/([\w-]+)/([\w-]+)/?$ pages/$2.php?sub_page=$3&$1=1 [L,NC,QSA]
RewriteRule ^(test)/([\w-]+)/([\w-]+)/?$ index.php?$1=1&page=$2&sub_page=$3 [L,NC,QSA]
## one level virtual directory without /test/
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([\w-]+)/?$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^([\w-]+)/?$ pages/$1.php [L]
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]
## two level virtual directory without /test/
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/?$ pages/$1.php?sub-page=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?page=$1&sub-page=$2 [L,QSA]
## three level virtual directory without /test/
RewriteCond %{DOCUMENT_ROOT}/pages/$1.php -f
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ pages/$1.php?sub-page=$2&sub-page2=$3 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ index.php?page=$1&sub-page=$2&sub-page2=$3 [L,QSA]