mod_rewrite重定向URL如果没有在主页上找到文件



我希望能够在没有.php,.html等的情况下访问我的脚本,所以我已经写了

RewriteEngine on
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php
##same for other extensions

在我的.htaccess文件中(注意:此文件不在根路上),但是我也想将每个不正确请求重定向到我的主页,以便www.mysite.com/dir/incorce将重写。www.mysite.com/dir/.

但是我的第一次尝试(重新写入后的重写 ^/[r])将我重定向到www.mysite.com/,我对rewriteBase(rewriteBase。和rewriteBase/)的实验无效,我也注意到许多类似的相似之处to wwwwwwwwwwwwwwwwwwww wwwwwww wwwwww wwwwwww wwwwww.mysite.com/dir/index.php(在我的情况下www.mysite.com/dir/index),但我真的想重定向到www.mysite.com/dir/。有什么办法可以实现这一目标?

以这种方式拥有它:

RewriteEngine on
# see if .php is found
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
RewriteRule ^(.*)$ $1.php [L]
# determine DIR_BASE dynamically
RewriteCond %{REQUEST_URI}::$1 ^(.*?/)(.*)::2$
RewriteRule ^(.*)$ - [E=DIR_BASE:%1]
# if not found redirect to %{ENV:DIR_BASE}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ %{ENV:DIR_BASE} [L,R]

最新更新