Hi Brothers,

I m completely new at PHP, i dont know anything about php
but from different sites i have come to know that i can redirect my unfriendly url to friendly urls. so i have started a short project but i dont know how to make url writing plz guide me.

I have 2 pages, 1)Index.php 2)cats.php
source code is for index.php is

<html>
<body>

<p><a href="http://www.example.com/cats.php?cat=1">category1</a></p>
<p><a href="">category2</a></p>
<p><a href="">category3</a></p>
<p><a href="">category4</a></p>
<p><a href="">category5</a></p>
<p><a href="">category6</a></p>

</body>
</html>

----------------------------------------------------------------------
and the source code for cats.php is

<html>
<body>
<?php
$txt=$_GET['cats'];
echo $txt;

?>
</body>
</html>

-----------------------------------------------------------------------------

I want to redirect
http://www.example.com/cats.php?cat=1

to



i have also updated .htaccess file by code provided by seochat.com from url rewriting code section


please tell me or provide me source files so i can make url rewriting

Dani AI

Generated

Practical, minimal recipe for friendly URLs (maps /cats/1 to cats.php?cat=1). Notes for : the posted cats.php has a small bug (wrong GET key) and the server must allow Apache rewrite rules. was right to point at the Apache side — confirm mod_rewrite is active and .htaccess files are being read.

Use this .htaccess (put in the site document root):

# .htaccess (web root)
RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cats/([0-9]+)/?$ cats.php?cat=$1 [L,QSA]

Explanation: the two RewriteCond lines avoid touching real files or directories. The RewriteRule captures a numeric id and internally rewrites /cats/123 to cats.php?cat=123. QSA preserves any additional query string; L stops processing further rules.

A safe, minimal cats.php to read the rewritten value (sanitize the input):

<?php
$cat = isset($_GET['cat']) ? intval($_GET['cat']) : 0;
if ($cat) {
    // load/display category data for $cat
    echo 'Category: ' . $cat;
} else {
    header('HTTP/1.0 404 Not Found');
    echo 'Category not found.';
}
?>

Generate links to the pretty URL (example):

<a href="/cats/1">category 1</a>

Extra tips and troubleshooting:

  • If nothing changes, check that mod_rewrite is enabled and Apache allows .htaccess (AllowOverride All).
  • Put any redirect from the old query URL to the pretty URL before the internal rewrite to avoid loops; use THE_REQUEST in the condition to detect the original request.
  • Watch server error logs for 500 errors (common when .htaccess syntax is wrong), and clear browser cache when testing 301 redirects.
  • Sanitize inputs (intval or casting) to avoid injection and use a 301 redirect for canonicalization if keeping both URL forms.

This gives a working, easy-to-understand baseline and highlights the common cause of silent failures: wrong GET key, disabled mod_rewrite, or .htaccess not being applied.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.