同一公共目录下CI4与WordPress的HTACCESS重定向循环问题求助
同一根目录下CI4与WordPress固定链接冲突解决
我在同一根目录部署了CodeIgniter 4(CI4)和WordPress程序,当将WordPress的固定链接设置为非默认格式时,会触发重定向次数过多错误。尝试添加规则排除WordPress目录但未解决问题,以下是相关信息及解决方案:
当前CI4的.htaccess代码
Options +FollowSymlinks RewriteEngine On # If you installed CodeIgniter in a subfolder, you will need to # change the following line to match the subfolder you need. # http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase RewriteBase / #RewriteRule ^(WP)($|/) - [L] # Redirect Trailing Slashes... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Rewrite "www.example.com -> example.com" RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] # Checks to see if the user is attempting to access a valid file, # such as an image or css document, if this isn't true it sends the # request to the front controller, index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA] # Ensure Authorization header is passed along RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
尝试过但无效的规则
RewriteRule ^(wordpress)($|/) - [L]
解决方案
问题根源是CI4的.htaccess规则会拦截WordPress的请求并转发到自身的index.php,导致循环重定向。需要将排除WordPress目录的规则放在CI4所有规则的最顶部,确保优先匹配放行:
修改后的CI4 .htaccess代码:
Options +FollowSymlinks RewriteEngine On # 优先放行WordPress目录的所有请求 RewriteRule ^wordpress($|/) - [L] RewriteBase / # Redirect Trailing Slashes... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} (.+)/$ RewriteRule ^ %1 [L,R=301] # Rewrite "www.example.com -> example.com" RewriteCond %{HTTPS} !=on RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] # Checks to see if the user is attempting to access a valid file, # such as an image or css document, if this isn't true it sends the # request to the front controller, index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA] # Ensure Authorization header is passed along RewriteCond %{HTTP:Authorization} . RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
注意事项
- 如果你的WordPress不是放在
wordpress子目录,需将规则中的wordpress替换为实际目录名称 - 确保WordPress目录下的.htaccess是其固定链接生成的默认规则(比如选择"文章名称"时自动生成的规则)
内容的提问来源于stack exchange,提问作者needmorecoffee
相关产品推荐
相关产品推荐

