Home
 

Prevent directory listing browsing using htaccess

1) Download your .htaccess file by using any ftp software.

2) Make sure you keep a backup of the file before making any change.

3) Open the file in any text editor.
Enter
Options -Indexes
at the end of the file and save it.

4) Upload the edited .htaccess file to your hosting account.

Now try to open a directory such as www.yourdomain.com/dir/. It should have been blocked by now.

Htaccess code to redirect from http to www

Add following code on your htaccess file to redirect http://yourdomain.com to http://www.yourdomain.com. Just change your domain name there, no other changes are required.

1
2
3
4
5
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]

Remove Canonial Feature from WordPress

Disable built-in canonical url function

in functions.php file # Remove WordPress’ canonical links

remove_action(‘wp_head’, ‘rel_canonical’);

Force SSL/https using htaccess

Often its required to redirect users of a site to secure https / ssl page. If you want to redirect whole website to secure website then you can use below code


RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

If you need to redirect users to a folder using https / ssl then use the code below.


RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} somefolder
RewriteRule ^(.*)$ https://www.yoursite.com/foldername/$1 [R,L]

htaccess to redirect root domain to a subfolder

If you want to redirect your domain yourdomain.com to yourdomain.com/blog then you need to add below code into a .htaccess file in your public_html folder.

1
2
3
4
5
6
7
Add the following lines to that file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomain.com$
RewriteRule ^(/)?$ blog [L]

Regular Expressions

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--?php <br ?--> $text = "Hello World";
if ( preg_match( "/r.*?d/", $text, $array ) ) {
print "
<pre>\n";
  print_r( $array );
  print "</pre>
\n";
}
?>

Output :
Array
(
[0] => rld
)

Get an IP address

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<!--?php <br ?--> $test = "126.122.95.52";
if ( preg_match( "/(\d+)\.(\d+)\.(\d+)\.(\d+)/", $test, $arr ) ) {
print "
<pre>\n";
  print_r( $arr );
  print "</pre>
\n";
}
?>

Output :
Array
(
[0] => 126.122.95.52
[1] => 126
[2] => 122
[3] => 95
[4] => 52
)

 
 
 

Categories

Tag