apache - 如何重写所有不包含短语的非 ssl 请求?

标签 apache .htaccess mod-rewrite ssl url-rewriting

我有一个特殊的问题。我需要重写所有转到 http => https 的请求,另外,如果 url 缺少 www,我想修复它,因此我也有一个重写规则。

现在,有一个异常(exception),不应该被重写。如果请求是为了 http://www.mydomain.com/ api2/.../..

api2下的一切都不应该重写为https...

这是我当前的 .htaccess :

RewriteEngine on

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

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

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [L]
# Start A2 Switcher Block
# Do not remove or modify this block! Added by PHP Switcher from cPanel to use an alterna$
<IfModule mod_suphp.c>
  AddHandler application/x-httpd-php-5.4.13 .php
</IfModule>
# End A2 Switcher Block

有人能帮忙吗?

最佳答案

让你的代码像这样:

RewriteEngine on

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^api2/ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

RewriteCond %{HTTPS} off
RewriteRule !^api2/ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

RewriteCond %{HTTPS} on
RewriteRule ^api2/ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^api2/ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NC]

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

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

# Start A2 Switcher Block
# Do not remove or modify this block! Added by PHP Switcher from cPanel to use an alterna$
<IfModule mod_suphp.c>
  AddHandler application/x-httpd-php-5.4.13 .php
</IfModule>
# End A2 Switcher Block

关于apache - 如何重写所有不包含短语的非 ssl 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23729283/

相关文章:

azure - .htaccess If/Else 语句不适用于 HTTP_HOST

django - Apache | Django : How to run websites on the back of a base URL?

apache - 使用 rewrite -phpmyadmin 在 URL 中的任何位置捕获不良行为

apache - Storm ui错误:org.apache.storm.utils.NimbusLeaderNotFoundException:从种子主机[localhost]中找不到领导者雨云

Apache 重写 : favicon in %{HTTP_HOST} directory

php - URL 重写后样式表不起作用

.htaccess - 将链接重定向到正确的目录

wordpress - 在 Wordpress 中通过 HTTPS 加载 CSS 和 JS

php - 服务器签名关闭在 .htaccess 上不起作用

security - 有没有办法强制apache返回404而不是403?