.htaccess导致重定向 [英] .htaccess causes redirection
问题描述
您好,我在public_html例如内部有2个文件夹(主文件夹和子文件夹).在我的public_html和2个文件夹相同的级别内,我有一个.htaccess文件,像这样.
Hello there I have 2 folders inside public_html ex.(mainfolder and subfolder). Inside my public_html same level with my 2 folders, I have an .htaccess file like this.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteOptions inherit
Options -Indexes
# php -- BEGIN cPanel-generated handler, do not edit
# Set the "ea-php73" package as the default "PHP" programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit
RewriteCond %{HTTP_HOST} ^paw\.xevz\.migor\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^www\.paw\.xevz\.migor\.eu$
RewriteRule ^/?$ "http\:\/\/paw\.xevz\.migor\.eu\/mainfolder" [R=301,L]
此.htaccess文件会将页面重定向到主文件夹.当我尝试以这种方式查看我的子文件夹时 http://paw.xevz.migor.eu/subfolder,一切运行良好,但是由于尝试查看某些功能时遇到了404错误,因为URL中没有index.php.我在子文件夹中添加了此.htaccess.
This .htaccess will redirect the page to mainfolder. When I try to view my subfolder like this http://paw.xevz.migor.eu/subfolder , everything is working well but since I encountered some 404 error when trying to view some functions because there is no index.php in the url. I added this .htaccess inside my subfolder.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
运行良好,但是我发现尝试打开时, http://paw.xevz.migor.eu/subfolder 像以前一样,它将重定向我到主文件夹.为了查看我的代码,我必须查看这样的链接. http://paw.xevz.migor.eu/subfolder/somefunction .有人可以解释为什么会这样吗?我对.htaccess不太了解,所以不确定发生了什么.为了避免这种冲突,我将在laravel项目或.htaccess中做什么?这样,当我要查看子文件夹时,需要将其放在url中? http://paw.xevz.migor.eu/subfolder 而不进行重定向.谢谢.
It is working well but I found out that when I try to open, http://paw.xevz.migor.eu/subfolder as I did before, it will redirect me to the main folder. In order to view my code, I have to view the link like this. http://paw.xevz.migor.eu/subfolder/somefunction. Can someone explain why this is happening ? I don't have much knowledge in .htaccess and I'm not sure what is going on. What will I do in my laravel project, or in my .htaccess to avoid this conflict ? so that when I want to view my subfolder, I will need to put this in the url ? http://paw.xevz.migor.eu/subfolder without redirecting . Thank you.
推荐答案
在您的.htaccess根目录中有此规则:
Have this rule in your root .htaccess:
# using THE_REQUEST make sure we match whitespace (\s)
# followed by 1+ / followed by ? or \s
RewriteCond %{THE_REQUEST} \s/+[?\s]
RewriteCond %{HTTP_HOST} ^(?:www\.)?paw\.xevz\.migor\.eu$ [NC]
RewriteRule ^ /mainfolder/ [R=302,L]
THE_REQUEST
变量表示Apache从浏览器收到的原始请求,并且在执行其他重写指令后不会被覆盖.此变量的示例值为 GET/index.php?id=123 HTTP/1.1
或 GET/HTTP/1.1
THE_REQUEST
variable represents original request received by Apache from the browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1
or GET / HTTP/1.1
在测试此更改之前,请确保清除浏览器缓存.
Make sure you clear your browser cache before testing this change.
这篇关于.htaccess导致重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!