问题
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