我正在研究mysite.com/dir/.htaccess中的.htaccess文件,该文件将mysite.com/dir/page重定向到mysite.com/dir/page.php(如果可能),并重定向到mysite.com/dir/不可能。因此,任何“错误”的请求都将被重定向到主页。我使用的代码是:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
RewriteRule ^(.*)$ $1.php
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [R]
这一切都很好。(顺便说一句:我想确定文件的基本目录,因为我可能想稍后更改目录名,但不要更改文件)。我现在想要的是为用户阻止一些目录,因为它们包含不应访问的php库。我如何才能像以前一样阻止目录mysite.com/dir/lib/*和mysite.com/dir/lib2/*并将它们重定向到mysite.com/dir/?我也想使用不喜欢的代码
RewriteRule ^lib/(.)* %{ENV:DIR_BASE}
这样会将mysite.com/dir/lib/../page重定向到主页,而不是应将其所属的mysite.com/dir/page重定向到主页。我对%{REQUEST_FILENAME}和%{REQUEST_URI}进行了很多尝试,但是当涉及到mod_rewrite时,我只是一个初学者。你知道解决方案吗?
这应该工作:
RewriteEngine on
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::\2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
RewriteRule ^lib2?(/|$) %{ENV:DIR_BASE} [L,NC,R]
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.*)$ $1.php [L]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句