PHP, MySQL, Drupal, .htaccess, Robots.txt, Phponwebsites: RewriteRule
RewriteRule - phponwebsites.com
Showing posts with label RewriteRule. Show all posts

19 Aug 2014

Create RSS Feed dynamically using PHP and MySQL

                                    You can create RSS feed dynamically using PHP like how you create sitemap dynamically using PHP and MySQL. Before you are going to know create RSS Feed, you should know what is RSS Feed? and why you need to add RSS Feed to your websites?

What is RSS Feed?


                                   RSS expands Really Simple Syndication. It is developed in XML like Sitemap. It is like content delivery vehicle. That means, you created a product, Ii distributes it to all places. Due to this feed, the viewers of your websites can easily find the latest pages of your websites. So there is no need to spend more time to search their desired topics on your web contents. This is the short description about RSS Feed. 

For more details, visit
    RSS Feed in W3schools
    RSS Feed at rss.softwaregarden
    what is RSS Feed
    Definition of RSS Feed at press-feed

Structure of RSS Feed

             
                                   The structure of RSS Feed is described below.


<?xml version='1.0' encoding='UTF-8'?>
<rss version='2.0'>
<channel>
    <title>Phponwebsites</title>
    <link>http://www.phponwebsites.com</link>
    <description>Phponwebsites about php related topics like .htaccess, robots.txt, mysql, jquery, ajax, js and also php for php developers</description>
    <language>en-us</language>
    <item>
      <title>PHP</title>
      <link>http://www.phponwebsites.com/p/php.html</link>
      <description>PHP doubts clarified at Phponwebsites</description>
   </item>
  <item>
      <title>MySQL</title>
      <link>http://www.phponwebsites.com/p/mysql.html</link>
      <description>MySQL doubts clarified at Phponwebsites</description>
   </item>
  <item>
      <title>htaccess</title>
      <link>http://www.phponwebsites.com/p/htaccess.html</link>
      <description>Htaccess doubts clarified at Phponwebsites</description>
   </item>
</channel>
</rss>

Generate RSS Feed dynamically using PHP and MySQL


                 Let see how to create RSS Feed dynamically using PHP and MySQL. Follow the below steps to create feed.


1. Create database and table


                  First you create database for store your link details on it. The below MySQL query is used for create database.

                Create database new

 where new is database name

                Use new

The above query is used to use database "new".
                  The MySQL query for create table is,

                Create table links (id int not null auto_increment primary key, name varchar(200), link varchar(200), description varchar(250))

                 Now the table "links" is created. Then store values into table using below query

                insert into table links values('','PHP','http://www.phponwebsites.com/p/php.html','PHP doubts are clarified at Phponwebsites')

                After insert values into table, your table look likes below

insert values into MySQL table for RSS Feed


2. Add header type RSS Feed


                            First you need to add content type of RSS feed. It is represented below.

header("Content-Type: application/rss+xml; charset=ISO-8859-1");


3. Add content to RSS Feed

                         
                            First of all you need to store all links in your database. Then on;y you can retrieve it using PHP. The below example we Retrieve data from MySQL database using mysql_fetch_array in PHP.
After added header, you have to add some name spaces for RSS Feed. Finally add contents to RSS Feed.


<?php
header("Content-Type: application/rss+xml; charset=ISO-8859-1");
echo '<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?>
<?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?>
<rss
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
    xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0"
version="2.0">
<channel>
<title>Phponwebsites</title>
<link>http://www.phponwebsites.com</link>
<description>Phponwebsites about php related topics like .htaccess, robots.txt, mysql, jquery, ajax, js and also php for php developers</description>';
       $connection = @mysql_connect('localhost', 'root', '') or die(mysql_error());
       mysql_select_db('new')  or die (mysql_error());
       $query = "SELECT * FROM links order by id desc";
       $rss = mysql_query($query) or die (mysql_error());
       $ImgPath='https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwiEMDL1tvD_oaHIph6KkufjJK5hc5m7kwdJoVgkjFJuf4oqWWbSQXNDkuq1-ukdOhgudTe6gou7Al4oAbvJ-oC-ejuZEhre-ue99v02aajGkKGacaDkZPfk09v7DvUzZlJbIoaxNajoMu/s1600/logo5.png';
       while($row=mysql_fetch_array($rss)){
       echo '
<item>
<title>'.$row['name'].'</title>
<link>'.$row['link'].'</link>
<content:encoded>
      <![CDATA[<p align="left">
          <a href="'.$row['link'].'"><img style="background-image: none; " border="0" src="'.$ImgPath.'" />    </a></p>]]>
       </content:encoded>
       <description>'.$row['description'].'</description></item>';
}
      echo '</channel>
</rss>';
?>

               When you run this file on firfox, you will get output like below

Create RSS Feed dynamicall using PHP and MySQL


4. Htaccess


                         Then you have to add below lines to your htaccess file.


                    RewriteRule ^rss.xml$ rss.php [L]

When you type rss.xml in your browser address bar, it redirects to rss.php.

             Finally you got dynamic RSS Feed.

Related Post:
Read data from RSS Feed using PHP
Create json file using PHP and MySQL
Create XML file using PHP and MySQL
PHP : Parse data from XML Sitemap using simplexml_load_file and file_get_elements
Read data from JSON file using PHP
Parse data from XML file using PHP

29 Nov 2013

How to access pages through one page in site using .htaccess

                        You can access all pages from your site through a one page. It can be possible in .htaccess. In this type, you have to specify the other page urls on that one page.

Syntax:
               RewriteRule ^(.*)$ index.php?url=$1 [L]

                    Where, when you try to access any pages in your site, at first your index.php file run. Then it
navigate to where you fixed it to go, it goes to that page. In your index.php should be like this:

                  $url=explode('/',$_GET['url']);
                 if($url[0]=='home.php') {
                   require_once('home.php');
                 }

                        where, you try to access your home.php file, first it comes to index.php and then check whether the home.php page is present or not. It navigates to home.php, if it is present in your site.

You can also hide all your files except home page:

Syntax:
               RewriteRule ^(.*)$ index.php

                  where, whatever you type in your address bar, you should be navigates to your index file.

Related Post:

27 Nov 2013

How to redirect to www using .htaccess

                       Type your domain name without www in address bar. Check whether it redirects to your site or give a 301 error message. If you got 301 error message, then you will solve this problem using .htaccess.

Syntax:
              RewriteEngine On
              RewriteCond %(HTTP_HOST) ! ^www.domainname.com$ [NC]
              RewriteRule ^(.*)$ http://www.domainname.com/$1 [L, R=301]

Explanation:
              Where,
                          .htaccess force the all http requests to use either www.domainname.com or domainname.com .
                           HTTP_HOST - represents your domain name.
                           RewriteCond   - check the conditions if you typed domain name is not start with www, it 
redirect to next rewrite rule. 
                           ! ^www.domainname.com$ - means you typed domain name is not start with www.
                           NC - represents not case sensitive.
                           RewriteRule - means it redirect domain name without www to http://www.doaminname.com.
                           ^ - starting
                           .* - one or more characters.
                           $ - ending
                           L - if rule matches, don't process any other rewrite rules below this one.

26 Nov 2013

How to redirect the urls using .htaccess

                        You can redirect the urls from one file name to another. It is possible in .htaccess. It can be done by 'RewriteRule' in .htaccess. The 'RewriteRule' is used to redirect the urls from one path to another. You can make your site urls very simple using RewriteRule method.


Rewrite urls using .htaccess:

                       You can rewrite the urls using .htaccess. When your address bar gets one page, the .htaccess can run another page. The .htaccess rewrite the urls from one page to another. Consider the following example.
Syntax:
                RewriteRule ^ex1.php example.php [L]

Explanation:
                First Rule:
                    when the ex1.php file is going to run, .htaccess file runs the example.php instead of ex1.php.

Hide file extension like php, html using .htaccess:

                             You can hide extensions like .php and .html using .htaccess. Consider the following  example.
                       
Syntax:
                RewriteRule ^home home.php [L]

Explanation:
                    When the browser get home to run, .htaccess runs the home.php instead of home. In this method, you can hide your file extensions like .html, .php .

Remove query string using .htaccess:

                               You can remove query string in address bar using .htaccess. There you can pass values in address bar. Consider the following example.


Syntax:
               RewriteRule ^example/(.*)$ example.php?ex=$1 [L]
Explanation:          
                     it avoids the normal rule.
      ie, example.php?ex=something
                 where,
                           when the browser get example.com/example/something, it considered as example.com/example.php?ex=something. Then you can get this value by $_GET['ex']. In this method, you can make your urls very simple and it is easily remembered by any browser.
                            ^  - starting point
                            .* -  one or more characters
                            $  - end point
                            L  - tells the server to stop trying to rewrite a URL after it has applied that rule.


How to redirect one web page url to another website:


                            Suppose you may have one more sites. If you feel when the users try to access the particular file, it directs to another your website.You can also redirect the urls from one website to another. It can be possible in .htaccess.

Syntax:
                Redirect  302 / site2.php  http://www.example2.com
Explanation:        
                    Where,  302 is the temporary redirect.
                                  When the user access the site2.php file in your current domain, it redirect to example2.com website.    
                         

How to redirect from one site to another:

                             
                                    Suppose you feel when the user access your one site, it redirect to another site. Then it can be done by .htaccess.

Syntax:
              Redirect 302 /   http://www.example2.com

              Now if the user come to your current domain, it will redirect to example2.com.


Related Post:

14 Nov 2013

What is .htaccess

                       Hypertext  access is shortly called as htaccess. It is a directory level configuration file. Directory level means, where you locate your .htaccess file, it configure that directory only. Mostly, on server, it is placed on public_html folder for configure the files to access. You can control your site using it. It is supported by web servers.
   

Uses of .htaccess:


why we use .htaccess

           - DirectoryIndex uses
           - used for restrict the user for access pages from website
           - redirect the urls
           - gives direction to server rather than search engine
           - create error document
           - compressing resources with gzip
           - add expires headers
           - enable and disable the additional functionality of apache webserver
           - prevent access to php.ini file
           - force scripts to display as a source code
           - ensure media files are downloaded instead of played
     

How to start with .htaccess?


   Syntax:
         <IfModule mod_rewrite.c>
             RewriteEngine On
             RewriteCond  %(REQUEST_FILENAME) !-f
             RewriteCond  %(REQUEST_FILENAME) !-d
             RewriteCond  %(REQUEST_FILENAME) !-l
             RewriteBase /
             DirectoryIndex index.php
             RewriteRule ^home index.php [L]
         </IfModule>
  Explanation:
     <IfModule mod_rewrite.c>
                                IfModule test whether mod_rewrite.c is included in apache. If mod_rewrite.c
is included, then it run. Otherwise it doesn't get run.
     RewriteEngine On
                                It tells apache to turn on its RewriteRules.
     RewriteCond
                                RewriteCond execute the next rewrite rules if it is correct.
%(REQUEST_FILENAME) is a variable based on url you request
!-f, !-d, !-l are extension added to regular expression.
where,
         !- f - rewrite condition if it is not  file
         !-d - rewrite condition if it is not directory
         !-l  - rewrite condition if it is not link
 if a file, taken from variable %(REQUEST_FILENAME) does not exist on the file system, then return true.
        RewriteBase /
                              It provide base for RewriteRules.                  
        DirectoryIndex index.php
                              It tells apache to which file is run first when server access to particular directory.
        RewriteRule
                              It redirect the urls.
RewriteRule ^home index.php [L]
      it means, when url get ' home ' on address bar, it runs the file index.php. Using this method, we can change our address bar contents to anything.
          For example,
                 RewriteRule ^sample.html sample.php [L]
                 where,
                            you type sample.html in your address bar, the .htaccess runs sample.php file.
  we can pass the values in address bar using it.
     
Related Post: