Do apache automatically disable www.?

For various reasons, such as cookies, SEO, and to keep things simple, I would like apache to automatically redirect any requests to http://www.foobar.com/anything to http://foobar.com/anything . The best I could come up with is a monster based on mod_rewrite, is there a simple easy way to say "Redirect all requests for ABC domain to XYZ"?

PS: I found this somewhat related question , but it is for IIS and does the opposite of what I want. Also it is still complicated.

+4
source share
6 answers

It's simple:

<VirtualHost 10.0.0.1:80> ServerName www.example.com Redirect permanent / http://example.com/ </VirtualHost> 

Assign host names and IP addresses if necessary :)

+9
source

simpler and easier to copy from site to site:

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

Pretty simple if you use mod_rewrite, like all of us;)

This is the .htaccess part from my website:

 RewriteEngine on # Catches www.infinite-labs.net and redirects to the # same page on infinite-labs.net to normalize things. RewriteCond %{HTTP_HOST} ^www\.infinite-labs\.net$ RewriteRule ^(.*)$ http://infinite-labs.net/$1 [R=301,L] 
+4
source

Use the .htaccess file with some mod_rewrite rules:

 RewriteEngine On RewriteRule ^www.SERVERNAME(.*) http://SERVERNAME$1 [L,QSA] 

I'm not sure I got the syntax with $1 , but it is well documented. L sends the location: header to the browser, and QSA means adding a query string.

+1
source

Since you mentioned the use of mod_rewrite, I would suggest a simple rule in your .htaccess - for me this is not monstrous :)

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

That should do the trick.

-1
source

Source: https://habr.com/ru/post/891855/


All Articles