htaccess

How to create an .htaccess file on Windows +

To create a .htaccess file, simply open a new blank document in your favorite text editor and save the file as .htaccess. Some text editors (like notepad) do not support the Unix file format and therefore you won´t be able to save a file with the ".htaccess" extension only. In this case you can simply save the file as 1.htaccess or htaccess.txt and later (after you have uploaded it to your server) rename it to .htaccess through your FTP client.

how to enable mod_rewrite module in apache in xampp, wamp? +

This is just a quick tutorial on how to enable how to mod_rewrite module in apache installed under windows environment.

1) Find the “httpd.conf” file under the “conf” folder inside the Apache’s installation folder.
2) Find the following line “#LoadModule rewrite_module modules/mod_rewrite.so” in the “httpd.conf” file.
   You can do this easily by searching the keyword “mod_rewrite” from find menu.
3) Remove the “#” at the starting of the line, “#” represents that line is commented.

Go to your apache module
Open httpd.conf

find this line 
  AllowOverride None
  
  Make it
  AllowOverride All
  
 Now restart the wamp server.

PHP Increase Upload File Size Limit +

Your php installation putting limits on upload file size. The default will restrict you to a max 2 MB upload file size. You need to set the following two configuration options:

  • upload_max_filesize - The maximum size of an uploaded file.
  • memory_limit - This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1.
  • post_max_size - Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. If memory limit is enabled by your configure script, memory_limit also affects file uploading. Generally speaking, memory_limit should be larger than post_max_size. There are two methods two fix this problem.

Method # 1: Edit php.ini

Edit your php.ini file (usually stored in /etc/php.ini or /etc/php.d/cgi/php.ini or /usr/local/etc/php.ini):

# vi /etc/php.ini

Sample outputs:

memory_limit = 32M
upload_max_filesize = 10M
post_max_size = 20M

Save and close the file. Restart apache or web server.

Method #2: Edit .htaccess

Edit .htaccess file in your root directory. This is useful when you do not have access to php.ini file. In this example, /home/httpd/html is considered as root directory (you can also create .htaccess file locally and than upload it using ftp / sftp / scp client):

# vi /home/httpd/html/.htaccess

Append / modify setting as follows:

php_value upload_max_filesize 10M
php_value post_max_size 20M
php_value memory_limit 32M

Save and close the file.

Custom Error Pages Using HTACCESS +

We can use custom error pages for any error as long as you know its number by adding the following code in htaccess file.

RewriteEngine On
ErrorDocument 404  /pageNotFound.html

In this htaccess rule if the file is not in the root directory of your site, url will be redirect to pageNotFound.html.

These are some of the common errors:

400 – Bad request
401 – Authorization Required
403 – Forbidden
404 – Wrong page
500 – Internal Server Error

How to protect your phpinfo and other sensitive files with htaccess +

If you are constantly referring to the file and would rather not delete it, consider adding the following slice of HTAccess to keep it private for your IP only:

# protect phpinfo
<Files php-info.php>
 Order Deny,Allow
 Deny from all
 Allow from 123.456.789
</Files>

Edit this snippet to include your specific IP address, along with any other IPs that may require access. Just use additional Allow from 123.456.789 lines to do so. Likewise, to protect other files, you can replace “php-info.php” with the name of the file, or use regular expressions to pattern-match specific file sets.

[htaccess] How to change your default Index page +

As a web developer, you will be well aware that default page under directory will be index.html. It is default assumption by any designer, developer or any web expert. But you might not aware that hosting provider sets default.html as default index page so whenever you open any url and if it has directory path, it will try to call default.html. If default.html will not be available than it will list all files under that directory and will not call index.html or index.php.

If you face such problem, you can overrite hosting default index page to whatever you want to set. If you want to set index.html as your default page than you can do it using htaccess. Set index.html as default index page using htaccess

Create .htaccess file and put it under root directory. Copy below code and paste it in the file.

 DirectoryIndex index.html 

Now it will call index.html as default index page. If index.html is not available under any directory, it will simple list all directory files. It might be case that you don’t have index.html under all directory and you might need to call index.php file for some. To do so, you can set index.html as first priority and if index.html is not available than it will call index.php file. Here is a code for that.

Set index.php as second default index page using htaccess

DirectoryIndex index.html index.php

You can set as many files as you want. It will start looking in sequence and if none of the file exist, it will list all files under that directory. Looking for how to set default index page ? index.html file is not calling, index.php file is not calling, index page does not works. All questions like these have one solution. I hope it helps you and saves your little time. Please share with your friends and let them know about the same.

How to Block IP Address using .htaccess +

Are you looking to block some IP address using .htaccess? If you are finding some IP address which you want to block and restrict access of your site, you can do the same using .htaccess. This is one of the good way to keep your site safe by blocking IP Address via .htaccess. Just write below piece of code in .htaccess and you will be done.

<Limit GET POST PUT>
order allow,deny
allow from all
deny from 214.53.25.64/26
</Limit>

If you want to block range of IP Address using .htaccess than write below code.

<Limit GET POST PUT>
order allow,deny
allow from all
deny from 214.53.25.64/255.255.255.192
</Limit>

Block IP Address using .htaccess, How to Block IP Address .htaccess, .htaccess code for IP Block, IP Address Ban .htaccess Code, .htaccess IP Address Ban Code can be achieved using above code.

Make PHP to work in your HTML files with .htacess +

By default most web servers across the internet are configured to treat as PHP files only files that end with .php. In case you need to have your HTML files parsed as PHP (e.g .html) or even if you want to take it further and make your PHP files look like ASP, you can do the following:

For web servers using PHP as apache module:

AddType application/x-httpd-php .html .htm

For web servers running PHP as CGI:

AddHandler application/x-httpd-php .html .htm 

In case you wish to do the ASP mimick:

For PHP as module:

 AddType application/x-httpd-php .asp 

OR For PHP as CGI:

 AddHandler application/x-httpd-php .asp 

Force www vs non-www to avoid duplicate content on Google +

Recently, it has been talked a lot about Google and duplicate content as well as Google Canonical problems.That is, when you have your site accessible both under your_domain.com and www.your_domain.com. To avoid such problems you can use the following lines in your .htaccess file to force only the www version of your web site:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.your_domain.com$
RewriteRule ^(.*)$ http://www.your_domain.com/$1 [R=301]

Please, note that the .htaccess should be located in the web site main folder.

This will redirect all requests to the non-www version of your site to the www version using 301 Permanent redirect which will make the search engines to index your site only using the www.your_domain.com URL. In this way you will avoid a duplicate content penalty.

Duplicate content fix index.html vs / (slash only) +

This similar to the www vs non-www version of your site work-around.

As you might now you, by default you can access your site as http://www.domain.com/ and http://www.domain.com/index.html with some setups it can be index.php or index.asp or default.aps, etc.

Unfortunately, this creates a risk of duplicate content from the search engine point of view. To avoid this, you can use the following ModRewrite rules in your .htaccess file:

RewriteEngine on 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
RewriteRule ^index\.html$ http://www.domain.com/ [R=301,L] 

The file should be located in your website main folder (web root).

The rules above will tell the browser or the search engine both that all requests to index.html should be directed to the "/" (slash only).

Hide .php extension with url rewriting using .htaccess +

When a search engine visits the dynamic url like product.php?id=5 it does not give much importance to that URL as search engine sees “?” sign treat it as a url which keeps on changing. so we’re converting the dynamic URL like the product.php?id=5 to static url format like product-5.html. We’ll rewrite the url in such a way that in browser’s address bar it will display as a product-5.html but it actually calls the file product.php?id=5. So that why these kind of URL also named as SEO friendly URL.

what is required for URL rewriting ??

To rewrite the URL you must have the mod_rewrite module must be loaded in apache server. And furthermore, FollowSymLinks options also need to be enabled otherwise you may encounter 500 Internal Sever Error. If you don’t know much about mod_rewrite module then please check this post to know how to check and enable mod_rewrite module in apache?

Examples of url rewriting for seo friendly URL

For rewriting the URL, you should create a .htaccess file in the root folder of your web directory. And have to put the following codes as your requirement.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [nc]

The following example will rewrite the test.php to test.html i.e when a URL like http://localhost/test.htm is called in address bar it calls the file test.php. As you can see the regular expression in first part of the RewriteRule command and $1 represents the first regular expression of the part of the RewriteRule and [nc] means not case sensitive.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ products.php?id=$1

The following example will rewrite the product.php?id=5 to porduct-5.html i.e when a URL like http://localhost/product-5.html calls product.php?id=5 automatically.

+

www.htaccess-guide.com

how-to-rewrite-urls-with-htaccess- PHP Gang