You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress站点.htaccess配置优化:解决重定向循环及URL规范问题

解决你的WordPress重定向循环与URL规范问题

嘿,我来帮你搞定这个ERR_TOO_MANY_REDIRECTS的坑,顺便实现你要的URL规范需求。先拆解下原代码的问题,再给你调整好的完整配置:

原代码的核心问题

  1. www转非www用了HTTP而非HTTPS:你明明部署了SSL,原规则却把www域名跳转到http://开头的地址,后续缓存规则里的HTTPS检查又会把HTTP转HTTPS,来回跳转就形成了循环。
  2. 缺少全局强制HTTPS规则:原代码的HTTPS检查嵌在缓存逻辑里,没有全局生效的强制跳转,导致部分请求可能绕开HTTPS。
  3. 完全没处理末尾斜杠:原代码里的条件甚至在检查URL是否以斜杠结尾,和你要移除斜杠的需求完全冲突。
  4. 重复开启RewriteEngine:WP Fastest Cache和WordPress块都开了RewriteEngine On,虽然不报错,但属于冗余配置。

修改后的完整.htaccess代码

# BEGIN WpFastestCache
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

# 1. 合并规则:强制HTTPS + 移除www(一次跳转完成,避免循环)
RewriteCond %{HTTP_HOST} ^www\.eniyibahiscasinositeleri\.com [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://eniyibahiscasinositeleri.com/$1 [R=301,L]

# 2. 移除URL末尾斜杠(排除真实目录,防止目录访问出错)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]

# Start WPFC Exclude
# End WPFC Exclude
# Start_WPFC_Exclude_Admin_Cookie
RewriteCond %{HTTP:Cookie} !wordpress_logged_in_[^\=]+\=eniyibah|sedatzekiekercili
# End_WPFC_Exclude_Admin_Cookie

# 保留WP Fastest Cache缓存逻辑,删除冲突的HTTPS检查条件
RewriteCond %{HTTP_HOST} ^eniyibahiscasinositeleri.com
RewriteCond %{HTTP_USER_AGENT} !(facebookexternalhit|WhatsApp|Mediatoolkitbot)
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_URI} !(\/){2}$
RewriteCond %{QUERY_STRING} !.+
RewriteCond %{HTTP:Cookie} !comment_author_
RewriteCond %{HTTP:Cookie} !wp_woocommerce_session
RewriteCond %{HTTP:Cookie} !safirmobilswitcher=mobil
RewriteCond %{HTTP:Profile} !^[a-z0-9\"]+ [NC]
RewriteCond %{DOCUMENT_ROOT}/wp-content/cache/all/$1/index.html -f [or]
RewriteCond /home/eniyibah/public_html/wp-content/cache/all/$1/index.html -f
RewriteRule ^(.*) "/wp-content/cache/all/$1/index.html" [L]
</IfModule>
<FilesMatch "index\.(html|htm)$">
AddDefaultCharset UTF-8
<ifModule mod_headers.c>
FileETag None
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Mon, 29 Oct 1923 20:30:00 GMT"
</ifModule>
</FilesMatch>
# END WpFastestCache
# BEGIN GzipWpFastestCache
<IfModule mod_deflate.c>
AddType x-font/woff .woff
AddType x-font/ttf .ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
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-font-ttf
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE font/opentype font/ttf font/eot font/otf
</IfModule>
# END GzipWpFastestCache
# BEGIN LBCWpFastestCache
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|webp|js|css|swf|x-html|css|xml|js|woff|woff2|ttf|svg|eot)(\.gz)?$">
<IfModule mod_expires.c>
AddType application/font-woff2 .woff2
ExpiresActive On
ExpiresDefault A0
ExpiresByType image/webp A2592000
ExpiresByType image/gif A2592000
ExpiresByType image/png A2592000
ExpiresByType image/jpg A2592000
ExpiresByType image/jpeg A2592000
ExpiresByType image/ico A2592000
ExpiresByType image/svg+xml A2592000
ExpiresByType text/css A2592000
ExpiresByType text/javascript A2592000
ExpiresByType application/javascript A2592000
ExpiresByType application/x-javascript A2592000
ExpiresByType application/font-woff2 A2592000
</IfModule>
<IfModule mod_headers.c>
Header set Expires "max-age=2592000, public"
Header unset ETag
Header set Connection keep-alive
FileETag None
</IfModule>
</FilesMatch>
# END LBCWpFastestCache
# BEGIN WordPress
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access plus 6 hours"
ExpiresByType image/jpeg "access plus 6 hours"
ExpiresByType image/gif "access plus 6 hours"
ExpiresByType image/png "access plus 6 hours"
ExpiresByType text/css "access plus 6 hours"
ExpiresByType application/pdf "access plus 1 week"
ExpiresByType text/javascript "access plus 6 hours"
ExpiresByType text/html "access plus 10 minutes"
ExpiresByType image/x-icon "access plus 1 year"
ExpiresDefault "access plus 3 hours"
</IfModule>
Header set X-Endurance-Cache-Level "2"
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

关键修改点说明

  • 合并HTTPS与www跳转:用OR条件一次性处理两种不规范的URL(www域名、HTTP访问),直接跳转到HTTPS裸域名,减少跳转次数还避免循环。
  • 新增末尾斜杠移除规则:先判断请求的不是真实目录(比如/blog/这种实际存在的目录不会被修改),再把带末尾斜杠的URL重定向到不带斜杠的版本。
  • 删除冲突的HTTPS检查:原缓存规则里的RewriteCond %{HTTPS} !=on已经没必要,因为全局已经强制HTTPS了,留着反而会干扰缓存逻辑。
  • 移除重复的RewriteEngine:WordPress块里的RewriteEngine On删掉,因为WP Fastest Cache块已经开启过了,精简配置。

额外注意事项

  1. 修改前一定要备份原.htaccess文件,万一出问题能快速恢复。
  2. 清空WP Fastest Cache的缓存,确保新规则立即生效。
  3. 去WordPress后台设置→常规里,把WordPress地址(URL)和站点地址(URL)都改成https://eniyibahiscasinositeleri.com,避免系统生成错误的URL。

内容的提问来源于stack exchange,提问作者Jay Stevens

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:35:21