The .htaccess file is a general text file, which can be create using Notepad or any text editor. This file contains the configuration statements/ commands to customize the Apache Web server as per user requirement.
if you try to create a .htaccess file in Windows it will not
allow you to do so because the file won't have a name. To solve
this problem you can create a file called htaccess.txt then
transfer it to your web server and rename it to .htaccess. Then
you can download the file, edit save and re upload.
or Very simple method open any editor like notepad go to
file save within double quotations write ".htacess" the file
will be created.
Also you can do it through command prompt.
Open Command Window (window key + r) type "command" press ok.
Now change to root directory. CD\ + enter
Type: copy con .htaccess press enter. Now press ctrl + z to save
the file.
You can check if your file is created or not.
.htaccess file helps you to Password protect you website, Deny/
Allow remote hosts or IP from your web site, Protecting your
images and other files from linking, Add Security to your PHP
projects, URL redirect/rewrite etc.
RewriteEngine On RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC] RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
301 redirect forcing all http requests to redirect from www.domain.com to domain.com
RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.domain\.com$ RewriteRule ^/?$ "http\:\/\/domain\.com\/" [R=301,L]
If you want to redirect all incoming URLs from domain.com/ to domain.com/index.html then use the following code.
RewriteEngine On RewriteCond %{HTTP_HOST} ^example.com$ RewriteRule ^$ http://example.com/index.php [L,R=301]
Like Yahoo some search engines remove the trailing slash from
URLs. So it creates duplicated content problems when the same
page content is accessible under different URLs. Per example
domain.com/google/ will index in Yahoo as domain.com/google -
which would result two different URLs with the same content.
To solve this problem lets add a trailing slash to the URLs
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} !example.php RewriteCond %{REQUEST_URI} !(.*)/$ RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_URI} (.*)$ RewriteRule ^(.+)/$ http://www.example.com/$1 [R=301,L]
RewriteEngine On RewriteCond %{HTTP_HOST} !oldexample.com$ [NC] RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]