Search code examples
linuxapacheseohttp-status-code-301

Redirect 301 www.www.example.com


I'm using LAMP server, and I need to redirect requests like:

www.www.example.com to www.example.com and other variations like (wwww.example.com, etc)

I know I can do it in .htaccess, but I don't know the regular expression that I should use to represent all these possibilities.

Or there is any diferent approach, comming from the vhosts?


Solution

  • I like:

    # force www IN URL
    RewriteEngine on
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
    # END force www IN URL
    

    Basically anything that's not www.example.com will get redirect 301'd.