1

I'm trying to redirect www.example.com to http://example.com using the .htaccess file in my root directory.

I've tried the examples listed here and elsewhere. rewriteengine is on.

rewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
rewriteRule (.*) //%1/$1 [L,R=301]

I've plugged this in in different spots of my .htaccess file, and I get redirect errors, saying it's improperly redirected.

In the above example, do I need to plug my actual site information in somewhere? If so, can someone give me an example using www.example.com?

I'm on a Linux based server, through GoDaddy.

1
  • You can't use a protocol-relative URL in the RewriteRule substitution. It's simply seen as relative to the document root (as is any URL that starts with a slash).
    – MrWhite
    Dec 10, 2016 at 10:35

2 Answers 2

1
RewriteCond %{HTTP_HOST} ^www.example.org$
RewriteRule ^(.*)$ http://example.org/$1
3
  • Joni, thanks. I entered in your code after inputting my domain info and I got a 500 Internal Server Error, however I noticed it was an Apache server. Any ideas?
    – Geo
    Jul 25, 2010 at 17:39
  • Can you see error logs? I don't know how to see them trough Godaddy.
    – Joni
    Jul 25, 2010 at 19:24
  • Got anything that also shows http/https depending on how they came in? Apr 27, 2012 at 1:19
0

You presently have

RewriteCond %{HTTP_HOST} ^www.(.) [NC]
RewriteRule (.) //%1/$1 [L,R=301]

I think you're almost there. Remember that in a regular expression a single dot . matches a single character. If you want to match one-or-more characters then try .+. And if you want to match zero-or-more characters then use .*. E.g.:

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

(you also need the leading http: for the redirect URL).

1
  • "(.)" - You only saw the single dots in the original question because it was incorrectly formatted. The pattern was really (.*) - the asterisk was simply hidden because it wasn't formatted as a code block (the markdown parser stripped it).
    – MrWhite
    Dec 10, 2016 at 10:24

You must log in to answer this question.