codeigniter在htaccess中向url添加www会阻止POST请求工作


codeigniter adding www to url in htaccess is stoping POST requests to work

我在将www添加到.htaccess中的url时遇到了一个相当奇怪的问题。我有一个基于编码的网站,并添加www到所有的url。但是我的Post请求已经停止工作了。这是我的apache .htaccess文件的内容

RewriteEngine on
#for adding www
RewriteCond %{HTTP_HOST} ^example'.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond $1 !^(index'.php|img|public) [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]

我使用$config['uri_protocol'] = 'AUTO';

,它确实工作与非www url,但它停止POST请求工作后添加www如上所述。我甚至尝试过REQUEST_URI,但它没有帮助。我还有一些其他的设置:

$config['base_url'] = 'http://example.com';

和自动加载$autoload['helper'] = array('url');

我猜问题是添加www后的302重定向不理解POST数据。

http://example.com

:

http://www.example.com

与其在。htaccess中使用301重定向,不如将该逻辑嵌入到应用程序中。

例如,对于每个请求,你可以这样做:

if (strstr($_SERVER['HTTP_HOST'], 'www') === FALSE) {
    $domain_with_www = 'http://www.example.com';
    header ('HTTP/1.1 301 Moved Permanently');
    header ("location: ".$domain_with_www.$this->uri->uri_string);
}

你可以通过扩展CI_Controller让它在每个请求上运行。更多信息请参见:http://codeigniter.tv/a-10/Extending-the-core-MY_Controller-and-beyond