How to Edit htaccess file to Improve Website?
WordPress is one of the best platforms to run a website. There are so many people hosting their websites by WP. Because you will get so many benefits from using WordPress. In WordPress of PHP sites, both have .htaccess file. So this file is very important to customize many things on your website. If you want to Redirect your URLs http to https, Non-www to www redirection, Fast load your website, and so many things you can do by .htaccess file. I believe so many of you don't know about the .htaccess file.
You can fix many common problems by this file. Or in simple terms .htaccess file are used to configure various web server settings such as cookie settings, redirect rules, cache control, access control, etc. In this article, we will inform you about some common problems you can fix using a .htaccess file.
What is .htaccess file?
Usually, .htaccess is a configuration file. You can do many configurations using the .htaccess file in your WordPress website, even in PHP-based websites. You can use.htaccess to do many valuable things, like control who can see what pages on your website, protect the admin area with a password, improve security, speed up your site, and move users. Modifying .htaccess file is not so difficult and not scary.
Where is the .htaccess file located?
The .htaccess file is located in the root of any website files directory. You can easily file the .htaccess file in your website's root directory. If you can't find the .htaccess file in the root directory of your website file manager you can search the file in your website root file manager. If you still have not found the file don't worry. You can manually create the file. and upload it to your root directory.
How to create .htaccess file?
Creating .htaccess file is so easy. If you can't find the .htaccess file in the root directory. Open any text editor in your PC after that copy the code from the below and paste it into the text file you have created now named this file htaccess and upload it to the root directory of your website and rename the file and add a full stop before htaccess name. That's it your .htaccess file has been created successfully.
How to Fix “The Link You Followed Has Expired” Error in WordPress
How to edit .htaccess file?
Editing .htaccess file in the hosting file manager is very easy. For editing .htaccess file from your file manager you don't have to download the file and re-upload it. You can directly edit the file from the File Manager. To edit the .htaccess file select the file and press the edit button from the navigation bar and after that a popup will show and from the popup menu press on edit.
Best Customization of .htaccess file.
There are a lot of things you can do and there are a lot of common problems you can fix with the .htaccess file. If you are searching for the best customization of the .htaccess file you are on the right site. In this article, we will let you know the best customization of .htaccess file.
- Redirect your URLs http to https.
- Non-www to www redirection.
- www to non www redirection.
- Redirect Domain to Sub-Directory
- Restrict Access to WordPress Admin Panel
- Block IP Address
- Disable Image Hotlinking
- Remove TRAILING Slash from your URL.
- Custom redirect any page to another page.
- Custom 404 Page redirection.
How to Redirect the URL http to https?
After installing the SSL successfully on your website you may see that your SSL is not working and your website link is not secure to fix this you can use .htaccess to redirect your URL HTTP to HTTPS. To do the settings open your .htaccess file in the edit mode. And paste the following code at the end of the .htaccess file. After placing the code don't forget to save the .htaccess file.
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

How to redirect Non-www to www in WP or PHP?
If your website not opening with www don't panic. This is not a big issue. You can fix non www to www redirect within a second. If your website is not opening with www you might have to trouble when you will try to add Google Search Consol setup to your website. So don't panic you can fix this problem by editing .htaccess file. To redirect non www to www open your .htaccess file in editing mode and paste the following code in the end of the .htaccess file. After adding the code you must have to save the file. The code is given below.
RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]

How to redirect www to non www?
If you want to redirect, your www added website to the non-www. You can do that by placing a short code in your .htaccess file. The code is given below. After add the code save the file.
RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC] RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
How to redirect Domain to Sub-Directory?
If you wish to Redirect your main domain to a subdirectory that can be done by .htaccess to redirect domain to subdirectory open your .htacess in edit mode. After that copy the code from below, paste it at the end of .htaccess, and save the file.
RewriteCond %{HTTP_HOST} ^example.com$ RewriteCond %{REQUEST_URI} !^/sub-directory-name/ RewriteRule (.*) /subdir/$1

How to Restrict Access to WordPress Admin Panel?
If someone got your WP access they can do whatever they want. Even they can shut down your website. There is a great feature on .htaccess file that is your WP admin can be accessed by a specific IP. Even if anyone knows your WP Access username and password no one can log in to your WP admin Panel. If you want to restrict access to the WP admin panel you can do that by adding a short code in the .htaccess file. The code is given below.
order deny,allow deny from all allow from xx.xx.xx.xx #whitelist techedubyte’s IP Address

How to block specific IP Addresses from WordPress?
If anyone tries to abuse, spam, hacking attempts to your website you can easily block the specific IP address by using your .htaccess file. Copy the code and paste the suspected IP address in the XX. XX.XX here and paste it to the end of .htaccess file and save the file.
Order Allow,Deny Deny from 123.45.67.89

How to Disable Image Hotlinking from WP?
This setting is so much important because when other people link your images directly to their website it will put additional strain on your hosting server. To prevent this image hotlinking you can add a simple code in you .htaccess file and save the file. The code is given below.
RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?example.com [NC] RewriteRule .(jpg|jpeg|png|gif)$ http://placeholder.png [NC,R,L]
How to remove extra / at the end of the website?
You might see an extra / at the end of your website. You can easily remove the extra / from your website URL using .htaccess code. To remove the extra / from the end of your website URL use the below code at the of .htaccess and save the file.
RewriteEngine on RewriteCond %{HTTP_REFERER} !^$ RewriteCond %{HTTP_REFERER} !^http(s)?://(www.)?example.com [NC] RewriteRule .(jpg|jpeg|png|gif)$ http://placeholder.png [NC,R,L]

How to add custom redirects any page to another page?
To redirect a specific page to another specific page, you can use the below code at the end of .htaccess and save the file
Redirect 301 /old-page https://example.com/new-page
How to do Custom 404 Page redirection.
There can be so many 404 errors pages on your website if you don't check your website regularly. If you found so many 404 error pages on your website, you want to fix them. If you trying to do manual redirection, it will so much time. You can do the 404 error page in one click using .htaccess. The 404 error all redirection code is given below copy the code and paste it at the end of .htaccess and save the file.
ErrorDocument 404 https://example.com/custom-404
FAQ
What if I will do something wrong in .htaccess file and dead the website?
There is nothing to worry about that. If you make any mistake delete the .htaccess file it will autogenerate a new .htaccess file. If the file is not generated automatically, make a new file as I show you above.
Conclusion
You can have more power over your WordPress site with the.htaccess file. You can set up redirects to your site's most recent pages, force SSL to send visitors to the HTTPS version of your site, and change other settings to make your WordPress site more private. If you like the article do share the article with your blogger friends. Or if you have more codes of htaccess share in the comment section, we will add the code in the article.