Category Archives: .htaccess

How protect files using php and .htaccess ?

phpmind-password-dialog

htaccess-logo
You can secure any file using this script.
It will not allow you to access without username and password.

1st step is to create .htaccess file in a folder which you want to protected.
2nd step is add index.php as shown in example. This will not work as it is but you can modify.

/*******************************************************/


.htaccess file

/********************************************************/

Options -Indexes 

RewriteEngine On

RewriteCond $0 !^(favicon\.ico|favicon\.png|media|robots\.txt|crossdomain\.xml|css|js)

RewriteRule .* index.php?file=$0 [QSA,L] # pass everything thru php
 

 


Share

How to enable cross-origin resource sharing using php

I was working on parsing KML data file from different resources which had different urls and Cesiumjs was unable to read/parse, that is how i come to know enabling cross-origin resource sharing.
It is also called CORS. Here is more details .

Following these steps you can enable “CORS”.
1. First check if you have apache module installed, using php.

  

2. Use command line to check if you have “Access-Control-Allow-Origin: *”

Command is - curl -I http://yourwebsite.com

3. If you don’t see this line “Access-Control-Allow-Origin: *” in curl -I output you need to add and enable it in either in PHP file ot .htacces file.

HTTP/1.1 302 Found
Date: Sat, 19 Sep 2015 00:31:41 GMT
Server: Apache
Access-Control-Allow-Origin: *
Set-Cookie: PHPSESSID=k1ejs4ra2gsu2id387h472one2; path=/
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma: no-cache
Location: https://phpmind.com/index.php
Content-Type: text/html; charset=UTF-8
Set-Cookie: BIGipServer~DART-Dev~DART=1241032896.20480.0000; path=/

4. Now its time to add code and enable your missing part!

In .htaccess

 

    Header set Access-Control-Allow-Origin "*"

In PHP file

header("Access-Control-Allow-Origin: *"); // for all

header('Access-Control-Allow-Origin: http://www.rnai.technology'); // For particular URL
header('Access-Control-Allow-Origin: http://phpmind.com');
header('Access-Control-Allow-Origin: http://maps.google.com');

After that you need to test using

Command is - curl -I http://yourwebsite.com

You must see Access-Control-Allow-Origin: *” with headers.

Share

How to add www in url through .htaccess?

Recently I was working on a project and SEO team requested to add WWW forcefully in project URL. I asked why, they said.
“Must have www in your URL! Google Like it.”
So here is .htaccess snippet to force a url with WWW
When you will type http://phpmind.com it will automatically become http://www.phpmind.com

RewriteEngine on

rewritecond %{http_host} ^phpmind.com [nc]
rewriterule ^(.*)$ http://www.phpmind.com/$1 [r=301,nc]

This is how I did in this site. I am telling you one .htaccess secret. You can experience here only.

Share

How to add and disable ETags through .htaccess?

What is ETag?
An ETag, or entity tag, is part of HTTP and it provides a unique identifier for the resource being supplied.

This identifier can then be used to validate if it resource has been modified or
Browser requests can use an etag and an if-match header to check whether a resource has been modified since it was served, providing an efficient caching facility that relies on content rather than on timeouts.

Use this code to setup ETags on your server, using following htaccess.


FileETag MTime Size

  
       ExpiresActive on
       ExpiresDefault "access plus 1 year"
   



Sometimes developer want to be disable ETags from headers. To disable ETags, make sure to include following snippet in your .htaccess file.


Header unset ETag
FileETag None

Share

How to use .htaccess for permanent redirect 301?

If you are making search engine friendly URL from pages which are using query string (i.e. www.phpmind.com?id=2 ) and pages which are indexed in search engines or if you are rewriting your website with new urls then you need permanent redirect 301!!

Let me tell you what happens here. If you remove a page which is already in Google it will generate error and that is not good for your site.
So .htaccess for permanent redirect will save life! Just redirect old to new url and Google will like your site this is good practice in terms of SEO too.

This one line PHP code is worth to add in .htaccess file of your root directory without loosing search engine ranking in Google and Others.

Redirect 301 /oldPage.html http://mydomain.com/newpage.html

Another Way 

redirectpermanent /contact-me.html http://www.phpmind.com/contact-us.html

Share

How to prevent hot linking using .htaccess?

Don’t let your users your photo steel! If you have very good photo or even not good! and want to prevent your files using php technology, such as jpg, gif, bmp, png etc.

You can just put this code in root directory of .htaccess file of your php project and it won’t let others site use you files directly and it will save your bandwidth. This is the main purpose of blocking hotlinking using .htaccess in php.


RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?my-website-url\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
RewriteRule .*\.(jpg|gif|bmp|png)$ http://phpmind.com/no-hotlinking-please.png [L]

Share

How to upload big file using .htaccess?

You can upload big file yes it is possible without applet and other third party lib!
Simply put these standard configuration values and they will overwrite php.ini setting, which does not let you upload big file in your server.

php_value upload_max_filesize 10M
php_value post_max_size 10M
php_value max_execution_time 300
php_value max_input_time 300

Share

How to force http to https through .htaccess?

In order to force https:// in payment and other secure pages even if your customer try to access http:// or they forget. You can use .htaccess file and simple code.

Example –
I had created a page http://www.phpmind.com/blog/
I want my visitor to access https://www.phpmind.com/blog/
Here is code which will force them to redirect to https://

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Share