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.

MrWhite
  • 12,647
  • 4
  • 29
  • 41
  • 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 '16 at 10:35

2 Answers2

1
RewriteCond %{HTTP_HOST} ^www.example.org$
RewriteRule ^(.*)$ http://example.org/$1
Joni
  • 111
  • 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? –  Jul 25 '10 at 17:39
  • Can you see error logs? I don't know how to see them trough Godaddy. –  Jul 25 '10 at 19:24
  • Got anything that also shows http/https depending on how they came in? – ServerChecker Apr 27 '12 at 01: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).

PP.
  • 3,316
  • 6
  • 27
  • 31
  • "`(.)`" - 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 '16 at 10:24