繁体   English   中英

为什么.htaccess无法从子文件夹正确重定向?

[英]Why does my .htaccess not redirect properly from a subfolder?

我创建了一个.htaccess文件,以从域中删除www前缀,并且还通过引导程序(index.php。)发送所有请求。这是:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

我的问题是,如果网站包含在子文件夹中,则重定向会将用户发送到根文件夹,并且重定向失败。

示例:用户访问“ www.example.com/website/news”->重定向到“ example.com/news”(应为“ example.com/website/news”)

任何人都有解决此问题的想法吗?

测试此规则

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subfolder/index.php?url=$1 [L,QSA]
</IfModule>

您可以使用REQUEST_URI变量而不是$1来解决此问题:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
</IfModule>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM