Forum Moderators: phranque

Message Too Old, No Replies

https non-www to www redirect

using mod_rewrite

         

jjump88

3:21 am on Aug 13, 2009 (gmt 0)

10+ Year Member



I want to do this: if they do [example.com...] I want to redirect them to [example.com...] (add the www.).

I have tried oodles of things to no avail.

RewriteCond %{REQUEST_URI} [example\.com...]
RewriteRule (.*) [example.com...]

Among using HTTP_Host and that didn't work either. I tried using HTTP_Host to see if www did not exist, then redir to [example.com....] This works if they do http://example.com but does not work with https (https://example.com).

Can anyone help?

jdMorgan

2:07 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have to refer to the correct variables to test specific request parameters. Here are several common solutions for use in .htaccess:

# Redirect https://example.com/<anything> to https://www.example.com/<anything>
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]

-or-

# Redirect non-www to www, preserving http/https protocol
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]

-or-

# Redirect all but canonical or blank hostname requests to www, preserving http/https protocol
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com/$1 [R=301,L]

Replace all broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim

jjump88

4:28 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



Thanks for your reply! Tried:

RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+s)$
RewriteRule ^(.*)$ http%2://www.example.com$1 [R=301,L]

This works on port 80 but not on port 443. So if we use http://example.com it redirects to [example.com...] and if we use example.com it redirects to [expample.com....]

BUT if we use [example.com...] it does NOT redirect to [wexample.com....] Any further suggestions?

jjump88

4:39 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



Additional info:

RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*)$ [example.com...] [R=301,L]

The above works if they do http://example.com

RewriteCond %{SERVER_PORT} =443
RewriteRule ^(.*)$ [example.com...] [R=301,L]

The above does NOT work if they do [example.com...] but I need it to.

Thanks again!

jdMorgan

5:14 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you putting this code where it will be executed for both servers?
And I used that phrase advisedly, because http and https are often mapped to two different virtual servers, with a separate DocumentRoot directory.

The code will need to go into a common directory, or you may need to copy it into the DocumentRoot directory .htaccess file of each server.

Jim

jjump88

6:32 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



The code is in httpd.conf

jdMorgan

6:36 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case make sure that if it *is not* enclosed in a <Directory> section, you add a leading slash to all patterns. For example, change $(.*)$ to ^/(.*)$

And again, if your setup defines two virtual servers, you'll need the code in both. However, if tht is the case, then the code won't need to test SERVER_PORT, because code for each vHost will only be run for that vHost.

Jim

jjump88

7:25 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



Tried

RewriteCond %{SERVER_PORT} =443
RewriteRule ^/(.*)$ [example.com...] [R=301,L]

Did not work. also tried:

#RewriteCond %{HTTP_HOST} ^https://example\.com
#RewriteRule ^(.*)$ [example.com...] [R=301,L]

Did not work.

The setup does not use virtual servers/hosts and it is the only domain.

jdMorgan

9:29 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, how is the SSL server implemented, then?

You can pretty much stop "trying code" as all of the routines I posted are tested/working routines. With the leading slash on the pattern (which you forgot on your commented-out snippet above), any of them should work; the only difference between them is as-documented in the comments. But you've either got the code in the wrong place, or you need to duplicate it in the SSL and non-SSL 'setups.'

Jim

jjump88

11:06 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



The virtual server portion of httpd.conf is commented out. There is an ssl.conf in conf.d that points to the same document root as in httpd.conf. ssl.conf points to the certs. Is that where I need to put the code?

Thanks so much for your help. This box was inherited and this is a bit new.

jjump88

2:45 pm on Aug 14, 2009 (gmt 0)

10+ Year Member



The code has been tried in ssl.conf httpd.conf and .htaccess still doesn't work. We are stumped and need to get this working.

Thanks again for the awesome help so far!

jdMorgan

2:58 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Stumped.
Yes, me too. :(

Be sure that all prerequisites for the use of mod_rewrite are met in both SSL and non-SSL cases:

  • Options +FollowSymLinks or Options +SymLinksIfOwnerMatch must be set.
  • If this Options directive appears only in .htaccess, then AllowOverride Options must be set in the .conf file(s) (possibly using AllowOverride All).
  • RewriteEngine on must be set in the .htaccess file.

    If you're getting any relevant errors or warnings in your server error log, please post them.

    Jim

  • jjump88

    3:17 pm on Aug 14, 2009 (gmt 0)

    10+ Year Member



    We put the code in the virtual directive inside ssl.conf and it worked! Thanks for the help!