【发布时间】:2020-09-10 03:24:01
【问题描述】:
这是我当前的 htaccess 文件。
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# Apache config
<FilesMatch ".(eot|ttf|otf|woff|php)">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
## EXPIRES CACHING ##
##<IfModule mod_expires.c>
##ExpiresActive On
##ExpiresByType image/jpg "access 1 year"
##ExpiresByType image/jpeg "access 1 year"
##ExpiresByType image/gif "access 1 year"
##ExpiresByType image/png "access 1 year"
##ExpiresByType text/css "access 1 month"
##ExpiresByType text/html "access 1 month"
##ExpiresByType application/pdf "access 1 month"
##ExpiresByType text/x-javascript "access 1 month"
##ExpiresByType application/x-shockwave-flash "access 1 month"
##ExpiresByType image/x-icon "access 1 year"
##ExpiresDefault "access 1 month"
##</IfModule>
## EXPIRES CACHING ##
我尝试添加
RewriteEngine On
RewriteCond %{HTTPS} !on$
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
但它不起作用。问题是根据当前的 htacess 文件,它将所有请求重定向到 index.php。 那么如何同时实现对 https 和 index.php 的重定向。 请帮我写代码。这将是一个很大的帮助
【问题讨论】:
-
这到底和php无关,请去掉那个标签。谢谢。
-
重定向必须在内部重写之前实现,所以在你的分布式配置文件(“.htaccess”)中更进一步。
-
@arkascha 谢谢。它解决了我的问题。我的问题的最终解决方案是
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?/$1 RewriteCond %{HTTPS} !=on RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] -
我写的重定向应该是之前实现的,所以上面内部重写了。你把它放在下面。这确实可行,但会导致额外的重定向。您可以使用 http 服务器的访问日志文件或浏览器控制台来确认。
-
是的,我看到了,我在评论中提到的上述代码无法正常工作。还有多个重定向。那么你能帮我写出正确的代码吗?
标签: redirect https http-redirect