Search code examples
php.htaccesshttp-redirectno-www

How to redirect from the www to the non-www version in an HTACCESS file?


I have installed the Laravel 5.2 in my Linux Server And I made it so hardly by modifying the HTACCESS file , So now I'm trying to make a redirection from the www to the non-www version I mean like stackoverflow from www.example.com >>TO>> example.com , But I'm afraid because I may crash my server so please help with the explanation of the code please :

RewriteEngine on 

# I changed my website real name to example
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 

RewriteCond %{REQUEST_URI} !^/example/ 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 

RewriteRule ^(.*)$ /example/$1 
RewriteCond %{HTTP_HOST} ^(www.)?example.com$ 
RewriteRule ^(/)?$ example/index.php [L]

Thanks in advance


Solution

  • You can try these rules:

    RewriteEngine on 
    
    # I changed my website real name to example
    RewriteCond %{HTTP_HOST} ^www\.(.+)$ 
    RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
    
    RewriteRule ^/?$ example/index.php [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^(?!example/)(.*)$ example/$1 [L,NC]