Some useful scripts for manage your site through the htaccess

Hello Friends!
Hope you are doing well :)
I am here going to tell you some useful script for manage .htaccess file of your website.

  1. First of all you have need to check the  RewriteEngine is “on” or not in your htaccess file. if not then add the given code in top of the file
    RewriteEngine on
  2. How redirect site form NON-WWW to WWW url using htaccess?
    ##non-www to www
    RewriteCond %{HTTP_HOST} !^www\.
    RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
  3. How redirect site from WWW to NON-WWW url using htaccess?
    pre>## www to non-www
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^(.*)$ %{HTTP_HOST}$1 [C]
    RewriteRule ^www\.(.*)$ http://$1 [L,R=301]
  4. How create a single line code for read “.php” files as “.html using htaccess”?
    ## Add rewrite rule for call ".php" pages as ".html" pages
    # Example about-us.php file will be read as about-us.html
    Options +Indexes
    Options +FollowSymlinks
    RewriteRule ^([^.]+)\.html$ $1.php [L]
  5. How remove the index.php OR index.html from the site url using htaccess
    RewriteRule ^(.*)index\.(htm|html|php)$ http://%{HTTP_HOST}/$1 [R=301,L]
  6. How redirect Old Site to new New Site?
      RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
      RewriteCond %{HTTP_HOST} ^www.mrwebsolution.com$
      RewriteRule (.*)$ http://www.mrwebsolution.com/$1 [R=301,L]
  7. How redirect all pages URL HTTP to HTTPS?
    RewriteCond %{SERVER_PORT} 80
    RewriteRule ^(.*)$ https://mrwebsolution.com/$1 [R,L]
  8. How add 301 redirection on all old pages that have a common suffix url to any other page?
    Example:
    mrwebsolution.in/go/news.release/id/43
    mrwebsolution.in/go/news.release&id=57
    These type of all url will be redirect to home page

    RewriteRule ^/?go/(.*)$ http://www.mrwebsolution.in/? [L,R=301]
  9. How add 301 redirection for sitemap.xml and robots.txt non-www url to www  url
    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(sitemap\.xml|robots\.txt)$  http://www.%{HTTP_HOST}/$1 [L,R=301,NC]
  10. How create new rewrite rule for custom page url
    RewriteEngine On
    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    RewriteBase /
    RewriteRule ^forsale/?$ index.php?SearchType=ByLocation&Purpose=1&IsFeatured=0 [NC,L]
    RewriteCond %{THE_REQUEST} ^.*/index.php
    RewriteRule ^(.*)index.php$ http://localhost/aqarynet/$1 [R=301,L]
    RewriteRule ^([^/]*)\.html$ /contents.php?pageid=$1 [L]
    RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /?SearchType=$1&Purpose=$2&IsFeatured=$3 [L]
    RewriteRule ^pid/([^/]*)\.html$ /property_details.php?pid=$1 [L]
    RewriteRule ^rid/([^/]*)\.html$ /realestate_details.php?rid=$1 [L]
    RewriteRule ^nid/([^/]*)\.html$ /news_details.php?nid=$1 [L]
    RewriteRule ^PageIndex/([^/]*)\.html$ /news.php?PageIndex=$1 [L]
    RewriteRule ^PageIndex/([^/]*)/rpp/([^/]*)\.html$ /realestate.php?PageIndex=$1&rpp=$2 [L]

Enjoy Code!

One Reply to “Some useful scripts for manage your site through the htaccess”

Leave a Reply

Your email address will not be published. Required fields are marked *