如何使用Apache规则阻止WordPress子目录上的HTTPS?

时间:2016-09-05 22:05:35

标签: wordpress mod-rewrite

我的目标是在用户尝试访问HTTP时将我网站的所有网页重定向到HTTPS ,除非正在访问的网页位于http://example.com/wc-api/下。 (包括wc-api的所有子目录。)

这些是我的规则:

RewriteEngine On

RewriteRule ^\/?wc-api.* - [END]

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

问题在于,虽然子目录中的所有网页都按预期重定向到https,但wc-api中的所有网页都会返回HTTP/1.1 404 Not Found

我希望这可能是因为子目录wc-api实际上并不存在。相反,它是WooCommerce的REST API。但是,如果我禁用所有这些规则,我可以访问/ wc-api /子目录,它可以按预期工作(尽管它没有实际存在)。

由于上述规则不起作用,我还尝试在HTTPS RewriteRule上使用否定模式条件:

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^\/?wc-api.*
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI}

然而,在这种情况下,页面随后被发送以由WordPress根目录中的.htaccess文件进行评估,然后由于此规则被重定向到我网站的索引页面:

RewriteRule . index.php [L]

这是我的WordPress .htaccess文件:

## Default .htaccess file

# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]

# END WordPress

# Wordfence WAF
<Files ".user.ini">
<IfModule mod_authz_core.c>
        Require all denied
</IfModule>
<IfModule !mod_authz_core.c>
        Order deny,allow
        Deny from all
</IfModule>
</Files>

# END Wordfence WAF

# BEGIN WP Performance Score Booster Settings

## BEGIN Enable GZIP Compression ##
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/x-httpd-php
AddOutputFilterByType DEFLATE application/x-httpd-fastphp
AddOutputFilterByType DEFLATE image/svg+xml
SetOutputFilter DEFLATE
</IfModule>
## END Enable GZIP Compression ##


## BEGIN Vary: Accept-Encoding Header ##
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|xml|gz)$">
Header append Vary: Accept-Encoding
</FilesMatch>
</IfModule>
## END Vary: Accept-Encoding Header ##


## BEGIN Expires Caching (Leverage Browser Caching) ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 2 week"
ExpiresByType image/jpeg "access 2 week"
ExpiresByType image/gif "access 2 week"
ExpiresByType image/png "access 2 week"
ExpiresByType text/css "access 2 week"
ExpiresByType application/pdf "access 2 week"
ExpiresByType text/x-javascript "access 2 week"
ExpiresByType application/x-shockwave-flash "access 2 week"
ExpiresByType image/x-icon "access 2 week"
ExpiresDefault "access 2 week"
</IfModule>
## END Expires Caching (Leverage Browser Caching) ##

# END WP Performance Score Booster Settings

我真的不知道该怎么做。我已经尝试了我能想到的每一种组合。为什么这不起作用?

1 个答案:

答案 0 :(得分:0)

这段代码怎么样?在我的环境中,它就像一个魅力:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule !^wc-api.* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]