How To Rewrite URL’s || The Basic Concept

Having simple and clean URL’s will add an advantage to your website. It helps search engines to understand the content of your website easily. Creating meaning full URL’s allow user’s to search the content on your website easily. URL rewriting plays a major role in producing dynamic sitemap (where sitemap.xml will be mapped to sitemap.php which produces sitemap dynamically). Now, it is possible to submit sitemap.xml to google rather submitting sitemap.php which is not possible. URL rewriting method is different for different type’s of hosting.

For Linux Server Hosting:

If your website is hosted on Linux server, then you need to use “.htaccess” file to rewrite the rules. Find “.htaccess” file on your root directory. if not found, create a file named “.htaccess” and simply add your rewrite rules into it.

A simple Rewrite Rule:
RewriteEngine On
RewriteRule   sitemap.xml   sitemap.php

For Windows Server Hosting:

“.htaccess” wont work on windows server. Instead you need to use “web.config” file in place of “.htaccess”. The below is the basic rewrite rule to map sitemap.xml to sitemap.php. Simply add the below code to “web.config” to make a rewrite rule.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.webServer>
  <rewrite>
   <rules>
    <rule name="rewrite sitemap">
     <match url="sitemap.xml" />
     <action type="Rewrite" url="sitemap.php" />
    </rule>
   </rules>
  </rewrite> 
 </system.webServer>
</configuration>
Develop sitemap.php such that it prints an xml format.
<?php
echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<urlset
 xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'
 xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:schemaLocation='http://www.sitemaps.org/schemas/sitemap/0.9
 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'>";
echo "<url><loc>http://www.domain.com/</loc></url>";
echo "<url><loc>http://www.domain.com/products/</loc></url>";
echo "</urlset>";
?>

Now, Open your browser and try loading “sitemap.xml”. You can see the content of “sitemap.php” loaded with the url “sitemap.xml”. Please comment your doubts.

Leave a Reply

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