0

I have host with multiple domain pointed. i need to remove 'www' part from requested URL and redirect to it.

Ex: 1. www.abc.com rediect to abc.com
    2. www.xyz.com redirect to xyz.com

i need to this dynamically.

I used below url re-write to do that but its not working.

RewriteCond %{HTTP_HOST} ^www\.%{REQUEST_URI}\.com$
RewriteRule ^/?$ "https\:\/\/%{REQUEST_URI}\.com\/" [R=301,L]

any help really appreciate.

  • Possible duplicate of [Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod\_Rewrite Rules but Were Afraid to Ask](http://serverfault.com/questions/214512/redirect-change-urls-or-redirect-http-to-https-in-apache-everything-you-ever) – Jenny D Apr 03 '17 at 12:35
  • The `REQUEST_URI` server variable is the requested _URL-path_, not the _second level domain_. (?!) – MrWhite Apr 03 '17 at 19:15

1 Answers1

1

You can try the following rules:

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

This will redirect www to non-www and it isapplies only for http URLs. You can change it to https if required. If you are writing this configuration in .htaccess file, you need to make you have the right AllowOverride config option. Otherwise, the .htaccess file will not be executed.

Khaled
  • 35,688
  • 8
  • 69
  • 98