What's the best way to redirect all URLs to one version ?

Joined
Dec 16, 2014
Messages
90
Likes
144
Degree
0
Hey Buso.

This is my first time having SSL on a site, and usually the www version of the site automatically redirects to the non www version, when I use wordpress.

This time I can go to to all 4 versions. The www the non www, the https www, and the https.

I want all URLs to be https without www.
Whats the best way to do that ?
 
Three questions, so we can provide you the most accurate answers:
  • What type of hosting is the site on (managed or unmanaged, shared or dedicated, etc.)?
  • What type of server (Apache, NGINX, etc.)?
  • What level of access and control do you have (can you edit .htaccess, nginx.conf, etc.)?
Generally speaking, what you'd want, depending on the answers to those questions, is a regex redirect. That will basically function as a "catch all", that will redirect anything that isn't the correct version, to one specific version of the site. The exact regex syntax may differ a bit, depending on the type of server, among other things.
 
What type of hosting is the site on (managed or unmanaged, shared or dedicated, etc.)?

It's shared namecheap hosting.

What type of server (Apache, NGINX, etc.)?

It seems to be Apache

  • What level of access and control do you have (can you edit .htaccess, nginx.conf, etc.)?

.htaccess doesnt seem to show up in the public html file manager.

I got cpanel. Also I got the namecheap premium dns, if that changes anything.
 
.htaccess doesnt seem to show up in the public html file manager.

When you click the File Manager, it offers you options like which file path to open to. One of those is a check box to show Hidden Files. That's what you want, and what the . refers to. It has no filename, just a file extension.

Alternatively you can use a FTP program (file transfer protocol) and find the same hidden files option in its settings. FileZilla was the jam on Windows back in the day. I've used Cyberduck on Mac for the past half a decade or more.
 
When you click the File Manager, it offers you options like which file path to open to. One of those is a check box to show Hidden Files. That's what you want, and what the . refers to. It has no filename, just a file extension.

Alternatively you can use a FTP program (file transfer protocol) and find the same hidden files option in its settings. FileZilla was the jam on Windows back in the day. I've used Cyberduck on Mac for the past half a decade or more.

Thanks found the htaccess. What now ? :D
 
@turbin3 @Ryuzaki

So should I set up redirects in the control panel of namecheap or is there a code I should be using in the htaccess ? Or are there even better ways for that ?
 
I won't lie. I resisted answering because Regex is a nightmare for me still. Give this a shot:

Code:
RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The first set of rules captures any "www" traffic and spits it over to the non-www and includes https.

The second batch checks any non-www traffic and makes sure its at https.

Second admitted lie: I used a StackOverflow search to help compile this answer.
 
I won't lie. I resisted answering because Regex is a nightmare for me still. Give this a shot:

Code:
RewriteEngine on

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

RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

The first set of rules captures any "www" traffic and spits it over to the non-www and includes https.

The second batch checks any non-www traffic and makes sure its at https.

Second admitted lie: I used a StackOverflow search to help compile this answer.

If you have a hard time, then I most likely would blow my brains out trying to figure it out.
Honesty is a noble thing, so thanks for that.

It seemed to work for the homepage, and the https www version also redirects to the correct url.
But the http and http www version dont redirect to the correct url for posts and pages.

Wouldnt this be something that could be done in the namecheap panel, by setting records or something ?
 
Wouldnt this be something that could be done in the namecheap panel, by setting records or something ?
No. It has to be done at the apache/nginx level (http server level). Here is the code that you need for your .htaccess:

Apache .htaccess:
Code:
#Switching www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

#Switches non-SSL to SSL
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

If you would rather have the preferred www instead of non-www switch the first set of code to
Code:
#Switches non-www to www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

--

For the non-savages, those running nginx.

nginx example.com.conf:
Code:
server {
	server_name example.com;
	listen 80;
	listen 443 ssl http2;

	ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

	#off on purpose
	#ssl on;

	rewrite ^ https://www.example.com$request_uri? permanent;
}

server {
	server_name www.example.com;

	#listen 80;
	listen 443 ssl http2;

	ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
	ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

	ssl on;
}

There is a bit more for NGINX since you have to add the code to run php, etc, and some of that is covered more in-depth here: Mastering Nginx Guide From A Newbie Perspective
 
Back