Redirect .HTACCESS重定向问题:URL Slug之前的index.php

Redirect .HTACCESS重定向问题:URL Slug之前的index.php,redirect,mod-rewrite,url-rewriting,.htaccess,Redirect,Mod Rewrite,Url Rewriting,.htaccess,我试图实现一个简单的http到https重定向和www到非www。问题是.htaccess将“index.php”放在url slug之前,导致服务器错误。发生的情况如下: 错误行为: http://example.com/url-slug -> https://example.com/index.php/url-slug http://example.com/url-slug -> https://example.com/url-slug 期望的行为: http://examp

我试图实现一个简单的http到https重定向和www到非www。问题是.htaccess将“index.php”放在url slug之前,导致服务器错误。发生的情况如下:

错误行为:

http://example.com/url-slug -> https://example.com/index.php/url-slug
http://example.com/url-slug -> https://example.com/url-slug
期望的行为:

http://example.com/url-slug -> https://example.com/index.php/url-slug
http://example.com/url-slug -> https://example.com/url-slug
注意:我希望所有查询重定向到主目录中的index.php页面,除非服务器上存在请求的文件。我希望在不更改浏览器中的url的情况下实现这一点,这会导致服务器崩溃

(目标:www->非www&http->https

当前.HTACCESS设置:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

我做错了什么?

您的
http到https
301重定向应该在其他内部重写规则之前位于顶部

RewriteEngine On
#http to https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) https://%1%{REQUEST_URI} [NE,L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
在测试此更改之前,请确保清除浏览器缓存