Below
are the some of the useful .htaccess codes for the cPanel / Linux based websites.
Default
.htaccess code for the WordPress Website :
#
BEGIN WordPress
<IfModule
mod_rewrite.c>
RewriteEngine
On
RewriteBase
/
RewriteCond
%{REQUEST_FILENAME} !-f
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
. /index.php [L]
</IfModule>
#
END WordPress
...........................................................................................................................
For Redirecting the domain from http to https:
...........................................................................................................................
.htaccess
code for the redirecting the domain to a sub folder:
RewriteEngine
On
RewriteCond
%{HTTP_HOST} ^(www\.)?domain\.com
RewriteCond
%{REQUEST_URI} !folder_name/
RewriteRule
^(.*)$ folder_name/$1 [L]
...........................................................................................................................For Redirecting the domain from http to https:
RewriteEngine
On
RewriteCond
%{SERVER_PORT} 80
RewriteRule
^(.*)$ https://www.example.com/$1 [R,L]
...........................................................................................................................
In
case you wish to force HTTPS for a particular folder you can use the following:
RewriteEngine
On
RewriteCond
%{SERVER_PORT} 80
RewriteCond
%{REQUEST_URI} somefoldername
RewriteRule
^(.*)$ https://www.domain.com/somefoldername/$1 [R,L]
...........................................................................................................................
To
provide the first priority for the index.html:
DirectoryIndex
index.html index.php
...........................................................................................................................
To
redirect the domain from non-www to www:
Options
+FollowSymLinks
RewriteEngine
on
RewriteCond
%{HTTP_HOST} !^www\.
RewriteRule
^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Or
you can also use the following code :
RewriteEngine on
RewriteBase /
RewriteCond
%{HTTP_HOST} !^www\.domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
...........................................................................................................................RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
To redirect the domain from www to non-www:
Options
+FollowSymLinks
RewriteEngine
on
RewriteCond
%{HTTP_HOST} ^www\.
RewriteRule
^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Or you can also use the following code :
RewriteEngine
On
RewriteBase
/
RewriteCond
%{HTTP_HOST} ^www.yourdomain.com [NC]
RewriteRule
^(.*)$ http://yourdomain.com/$1 [L,R=301]
...........................................................................................................................
Blocking
the IP using the htaccess:
Example:
Block access from all except 1.2.3.4 and the 5.6.7.0 class C:
order
deny,allow
deny
from all
allow
from 1.2.3.4
allow
from 5.6.7.0
...........................................................................................................................
To
redirect the single page:
Redirect
301 /oldpage.html http://www.yoursite.com/newpage.html
...........................................................................................................................
Redirect
one domain to another domain:
<IfModule
mod_rewrite.c>
RewriteEngine
On
RewriteCond
%{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond
%{HTTP_HOST} ^www.olddomain.com$
RewriteRule
(.*)$ http://www.newdomain.com/$1 [R=301,L]
</IfModule >
...........................................................................................................................
Upgrading
the php version to php 5.3 :
Action
application/x-httpd-php53 /cgi-sys/php53
AddHandler application/x-httpd-php53 .php
AddHandler application/x-httpd-php53 .php
or
Action application/x-hg-php53 /cgi-sys/php53
AddHandler application/x-hg-php53 .php
Upgrading
the php version to php 5.4
AddHandler application/x-httpd-php54 .php .php5 .php4 .php3
AddType application/x-httpd-php54 .php
OR
OR
#Use
PHP 5.4
AddHandler
application/x-httpd-php54 .php
<IfModule
mod_suphp.c>
suPHP_ConfigPath
/opt/php54/lib
</IfModule>
Upgrading the php version to php 5.5
#Use
PHP 5.5
AddHandler
application/x-httpd-php55 .php
<IfModule
mod_suphp.c>
suPHP_ConfigPath
/opt/php55/lib
</IfModule>
...........................................................................................................................
To
redirect from https to http:
RewriteCond
%{SERVER_PORT} ^443$
RewriteRule
^(index\.html)?$ http://www.example.com/ [R=301,L]
...........................................................................................................................
Password
Protect the Directory:
<IfModule
mod_rewrite.c>
RewriteEngine
On
RewriteCond
%{QUERY_STRING} !=""
RewriteCond
%{QUERY_STRING} !^p=.*
RewriteCond
%{QUERY_STRING} !^s=.*
RewriteCond
%{REQUEST_URI} !^/wp-admin.*
RewriteRule
^(.*)$ /$1? [R=301,L]
</IfModule>
...........................................................................................................................
To
prevent the .htaccess of the primary domain affecting the addon
domain,place the following code in addon domain's .htaccess file:
RewriteEngine
On
...........................................................................................................................
Laravel
htaccess code:
#RewriteOptions
inherit
<IfModule
mod_rewrite.c>
<IfModule
mod_negotiation.c>
Options
-MultiViews
</IfModule>
RewriteEngine
On
#
Redirect Trailing Slashes If Not A Folder...
#RewriteCond
%{REQUEST_FILENAME} !-d
RewriteRule
^(.*)/$ /$1 [L,R=301]
#
Handle Front Controller...
RewriteCond
%{REQUEST_FILENAME} !-d
RewriteCond
%{REQUEST_FILENAME} !-f
#
RewriteRule ^ public/index.php [L]
RewriteRule
^(.*)$ public/$1 [L]
</IfModule>
...........................................................................................................................
No comments:
Post a Comment