Solve 7 SEO issues such as www to non-www & vice versa, fasten the website, add/remove trailing slash with .htaccess file.
HTTP to HTTPS
Redirect
The first task is to
redirect HTTP URLs to HTTPS URLs. Copy the provided code and paste it into your
.htaccess file. Replace example.com with your domain name. This will redirect
HTTP URLs to HTTPS URLs using a 301 redirect, as specified in the code.
RewriteEngine On
RewriteRule ^(.*)$
https://example.com/$1 [L,R=301]
WWW to Non-WWW
Redirect
The second task is to
redirect non-www URLs to www URLs or vice versa. Again, replace example.com
with your domain name and copy and paste the provided code into your .htaccess
file. The code checks the domain name and adds www to URLs that do not have it.
www to non-www
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
non-WWW to WWW
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Add/Remove Trailing
Slash
The third task is to add
or remove trailing slashes from URLs. Although these two URLs may look the
same, Google considers them as two different URLs. You can choose to use URLs
with or without trailing slashes, but it is essential to stick to one pattern.
The code provided will help you achieve this task.
ADD TRAILING SLASH
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
REMOVE TRAILING SLASH
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)/$ $1 [L,R=301]
REDIRECT ONE PAGE
TO ANOTHER
To redirect a URL,
simply paste one line of code into your .htaccess file that includes both the
old and new URLs. This simple solution can help you avoid using any plugins and
can save you time and effort.
Redirect 301 /old-page
https://example.com/new-page
REMOVE EXTENSION
(.html, .php. or .aspx) FROM URL
If you want to remove
.html or .aspx from the end of your URL and give it a clean look, you can use
this code. This code is for removing .html, but if you want to remove .php or
.aspx instead of .html, just replace it and it will work.
RewriteEngine on
RewriteCond
%{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1
[NC,L,R=301]
RewriteCond
%{REQUEST_FILENAME}.html -f
RewriteRule ^
%{REQUEST_URI}.html [NC,L]
REWRITE THESE TYPE
URLs
You can use this code to
make these types of URLs user-friendly.
Old URL: example.com/article.php?id=123
New URL: example.com/article/123
Now, this change is a
bit tricky because the code will change based on the pattern of these URLs, but
still, you get an idea that it’s possible.
RewriteEngine On
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
^article/([^/]+)$ /article.php?id=$1 [L]
CUSTOM 404 PAGE
.htaccess file can also
be used to assign a custom page as a 404 error page. You don’t need any plugins
for this, simply create a custom page on your website and paste the code into
your .htaccess file, replacing the example URL with your own.
ErrorDocument 404
https://example.com/custom-404
INCREASE THE SPEED
OF THE WEBSITE
You can use the
.htaccess file to increase your website’s speed by implementing the browser
cache method. Paste the code into your .htaccess file, and your website will
load faster for returning visitors.
<filesMatch
“.(css|jpg|jpeg|png|gif|js|ico)$”>
Header set Cache-Control
“max-age=2592000, public”
</filesMatch>
What We Learned So
Far:
The .htaccess file is a
powerful tool that can help you save a lot of work and headache in solving
these issues.
Reference:
Download code:
No comments:
Post a Comment