.htaccess无法在子目录

时间:2015-07-22 18:25:47

标签: php regex apache .htaccess mod-rewrite

我的.htaccess文件应删除网址中的.php扩展名,强制斜杠并在需要时指定get值。如果找不到脚本/文件/目录,它会将您重定向到根目录。例如,如果您输入http://127.0.0.1/about-us/它会将您带到http://about-us.php,或者如果您输入http://127.0.0.1/file/delete-avatar/230,它会重写为http://127.0.0.1/file.php?a=delete-avatar&id=230,或者如果您输入http://127.0.0.1/nu(其中nu是现有目录)它将您带到http://127.0.0.1/nu。但是,当您将脚本放在目录中并附加GET值时,.htaccess将无法重写并重定向到索引。例如,如果您键入http://127.0.0.1/nu/login/recover-password,则不会重写为http://127.0.0.1/nu/login?a=recover-password。我的.htaccess就在这里

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /     

   # remove php extensions (only for GET method)
   RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php(?:\?|\s) [NC]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule ^ %1/? [L,R=301]

   # don't touch other existing files/folders
   RewriteCond %{REQUEST_FILENAME} -f [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^ - [L]

   # force trailing slash
   RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
   RewriteRule ^(.+)$ $1/ [L,R=301] 

   # rewrite extensionless php files
   RewriteCond %{DOCUMENT_ROOT}/$1.php -f
   RewriteRule ^(.+)/$ $1.php [L]

   #subscribe
   RewriteRule ^([^/]+)/([^/]+)/?$ $1.php?a=$2 [QSA,L]
   RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ $1.php?a=$2&id=$3 [QSA,L]

   # finally, if not found
   RewriteRule ^ index.php [L]
</IfModule>

1 个答案:

答案 0 :(得分:1)

#subscribe

下面插入新规则
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+/[^/]+/[^/]+)/([^/]+)/?$ $1.php?a=$2 [L,QSA]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+/[^/]+)/([^/]+)/?$ $1.php?a=$2 [L,QSA]