Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ecollado's avatar

Best way to strip "www" from URL?

I'm deploying my first Laravel app and realized that http://example.com is treated as a different site than http://www.example.com with regard to setting the session and cookies. So I want to strip the "www" so that there's no confusion with the users. What's the best way to do this?

The examples I found online require specifying a specific domain within .htaccess, but I'd prefer to do something generic (if possible) so that the app behaves appropriately without me having to have separate .htaccess files for each domain that uses this. Is that possible?

I did find one example of doing this in a generic way, and it worked, but it clashed with Laravel's other .htaccess settings. The URLs would come out as http://example.com/index.php/page instead of http://example.com/page.

Any help is appreciated. Thanks!

0 likes
4 replies
Ozan's avatar
Ozan
Best Answer
Level 32

If you are using Apache:

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

.htaccess will do just fine.

If you are using Nginx:

HTTP Solution

From the documentation, "the right way is to define a separate server for example.org":

server {
    listen       80;
    server_name  example.com;
    return       301 http://www.example.com$request_uri;
}

server {
    listen       80;
    server_name  www.example.com;
    ...
}

HTTPS Solution

For those who want a solution including https://...

server {
        listen 80;
        server_name www.domain.com;
        # $scheme will get the HTTP protocol
        # and 301 are best practice for tablet, phone, desktop and seo
        return 301 $scheme://domain.com$request_uri;
}

server {
        listen 80;
        server_name domain.com;
        # here goes the rest of your config file
        # example 
        location / {
            
            rewrite ^/cp/login?$ /cp/login.php last;
            # etc...
            
        }
}

Note: I have not originally included https:// in my solution since we use load balancers and our https:// server is a high-traffic SSL payment server: we do not mix https:// and http://.


To check the Nginx version, use nginx -v.

Strip www from url with nginx redirect

server {
    server_name  www.domain.com;
    rewrite ^(.*) http://domain.com$1 permanent;
}

server {
    server_name  domain.com;
    #The rest of your configuration goes here#
}

So you need to have TWO server codes.

Add the www to the url with nginx redirect

If what you need is the opposite, to redirect from domain.com to www.domain.com, you can use this:

server {
    server_name  domain.com;
    rewrite ^(.*) http://www.domain.com$1 permanent;
}

server {
    server_name  www.domain.com;
    #The rest of your configuration goes here#
}

As you can imagine, this is just the opposite and works the same way the first example. This way, you don't get SEO marks down, as it is complete perm redirect and move. The no WWW is forced and the directory shown!

Some of my code shown below for a better view:

server {
    server_name  www.google.com;
    rewrite ^(.*) http://google.com$1 permanent;
}
server {
       listen 80;
       server_name google.com;
       index index.php index.html;
       ####
       # now pull the site from one directory #
       root /var/www/www.google.com/web;
       # done #
       location = /favicon.ico {
                log_not_found off;
                access_log off;
       }
}
1 like
ecollado's avatar

Thanks @Ozan! I'm using Apache and that works beautifully. I could've sworn I came across that same exact snippet in my search. Not sure what I did wrong the first time, but it works now. Again, many thanks!

Please or to participate in this conversation.