[英].htaccess not working in subdirectory
My .htaccess file should remove .php extensions in a url, force a slash and assign get values where required. 我的.htaccess文件应删除网址中的.php扩展名,强制使用斜杠并在需要时分配获取值。 Where a script/file/directory is not found, it redirects you to the root. 如果找不到脚本/文件/目录,它将把您重定向到根目录。 For example it works perfectly if you type http://127.0.0.1/about-us/
it takes you to http://about-us.php
or if you type http://127.0.0.1/file/delete-avatar/230
it rewrites to http://127.0.0.1/file.php?a=delete-avatar&id=230
, or if you type http://127.0.0.1/nu
(where nu is an existing directory) it takes you to http://127.0.0.1/nu
. 例如,如果您输入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/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
。 However when you put a script inside a directory and append a GET value the .htaccess fails to rewrite and redirects to the index. 但是,当您将脚本放在目录中并附加GET值时,.htaccess无法重写并重定向到索引。 For example, if you type http://127.0.0.1/nu/login/recover-password
does not rewrite to http://127.0.0.1/nu/login?a=recover-password
. 例如,如果您键入http://127.0.0.1/nu/login/recover-password
不会重写为http://127.0.0.1/nu/login?a=recover-password
。 My .htaccess is here 我的.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>
Insert this new rule just below #subscribe
line: 在 #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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.