I have this in my .htaccess file:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) http://www.example.com$1 [R=301,L]
but whenever I access a file on my root like http://example.com/robots.txt
it will redirect to http://www.example.comrobots.txt/
.
How can I correct this so that it will redirect correctly to http://www.example.com/robots.txt
?
This configuration worked for me in bitnami wordpress with SSL configured :
Added the below under "RewriteEngine On" in file /opt/bitnami/apps/wordpress/conf/httpd-app.conf
Here's the correct solution which supports https and http:
If possible, add this to the main Apache configuration file. It is a lighter-weight solution, less processing required.
So, the separate
VirtualHost
for "example.com" captures those requests and then permanently redirects them to your mainVirtualHost
. So there's no REGEX parsing with every request, and your client browsers will cache the redirect so they'll never (or rarely) request the "wrong" url again, saving you on server load.Note, the trailing slash in
Redirect permanent / http://www.example.com/
. Without it, a redirect fromexample.com/asdf
would redirect tohttp://www.example.comasdf
instead ofhttp://www.example.com/asdf
.I believe the top answer successfully redirects non-www to www (ex: mysite.com -> www.mysite.com), but doesn't take into account wildcard subdomains, which results in:
Here's a solution with/without HTTPS
HTTP
HTTP/HTTPS
*note: I haven't tested https because I don't currently have a cert to test, but if someone can verify or optimize what I have that would be awesome.
For Https
Write in .htaccess :)