redirecting-to/adding www in url codeigniter based project

旧城冷巷雨未停 提交于 2019-12-24 19:09:53

问题


I am trying to add www and redirect all non-www urls to www urls in a codeigniter-based project, but my post data is not working after doing so. Here is the content of the Apache .htaccess file:

RewriteCond %{HTTP_HOST} ^examplesite\.com$ [NC]
RewriteRule ^(.*)$ http://www.examplesite.com/$1 [R=301,L]

I have $config['uri_protocol'] = 'AUTO';.

Everything works like a charm if I do not have www urls. I have tried REQUEST_URI, but it didn't work either.


回答1:


This is because browsers wont resubmit POST data if the response is a redirect. This is a normal and expected thing. What you can do is to only rewrite GET requests with an extra condition:

RewriteCond %{REQUEST_METHOD} GET [NC]
RewriteCond %{HTTP_HOST} ^examplesite\.com$ [NC]
RewriteRule ^(.*)$ http://www.examplesite.com/$1 [R=301,L]

Leave the $config['site_url'] option in application/config/config.php to an empty string (that will make CI do autodetect) and in your view code, use the site_url() helper or the form builder functions so it will use the domain that the request came in.



来源:https://stackoverflow.com/questions/17303964/redirecting-to-adding-www-in-url-codeigniter-based-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!