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

时间:2020-01-13 18:46:32

标签: redirect mod-rewrite url-rewriting .htaccess

我正在尝试实现一个简单的http到https重定向和www到非www的实现。问题是.htaccess将“ index.php”放在url段之前,导致服务器错误。这是发生了什么:

错误的行为:

http://example.com/url-slug -> https://example.com/index.php/url-slug

期望的行为:

http://example.com/url-slug -> https://example.com/url-slug

注意:我希望所有查询都重定向到主目录中的index.php页面,除非请求的文件存在于服务器上。我想在不更改浏览器网址的情况下实现此目的,而这会导致服务器崩溃。

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

当前。成功设置:

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]

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您的http to 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]

在测试此更改之前,请确保清除浏览器缓存。

相关问题