I need to redirect specific URLs to their new equivalent.
i have a single dynamic page(pages.php) and i fetch data from database.(like domain_name.com/pages.php?id=4)
and i want to redirect to
domain_name.com/pages/name
or
domain_name.com/name

on same page with different names(for example if pages.php id=4(example- domain_name.com/pages.php?id=4) and id 4 have name welcome then url goes
domain_name.com/welcome
or
domain_name.com/pages/welcome
or
if pages.php have id=5(domain_name.com/pages.php?id=5) and id 5 have name faqs then url goes domain_name.com/faqs or
domain_name.com/pages/faqs
will you plz help me

Recommended Answers

All 5 Replies

You should have a "id - title" map which can be retrieved from database. After finding title which is assigned to id, simply run something like below to HTTP redirect:

<?php
$title = getTitleFromSomewhereById($_GET['id']);
header("Location: http://domain_name.com/pages/" + $title);
// Set other headers if you like
exit;
?>
Member Avatar for diafol

As you reference via 'id', you're probably stuck with this php header. However, if you reference:

domain_name.com/pages.php?id=faq

domain_name.com/pages.php?id=welcome

You could use Apache mod rewrite to:

domain_name.com/faq OR domain_name.com/pages/faq

domain_name.com/welcome OR domain_name.com/pages/welcome

I'm assuming that you have DB or array info linking the id number to the page name, so simply use the name instead of the number when you create your dynamic links. So if you have an .htaccess file as at the end of this post, then you could build your links to this:

domain_name.com/pages/faq/

and then you'd pick the $_GET['id'] in the pages.php file which would give 'faq'. The .htaccess file would look like this:

RewriteEngine On
RewriteRule ^pages/([^/]*)/$ /pages.php?id=$1 [L]

thankx for your response but it was not work.

my php code is:

<ul>
<?php foreach($moreinfo as $moreinfoarr){if($moreinfoarr['pageId'] !=9) {?>
                <li><a  href="pages.php?id=<?php echo $moreinfoarr['pageId'];?>"><?php echo $moreinfoarr['menutitle'];?></a></li>
                <?php } }?>
                                </ul>





$moreinfo was function to fetch data from database by quarry
like
$sql="select * from tablename where status='1'";
$result=mysql_query($sql);
while($moreinfo=mysql_fetch_assoc($result))
{
........
}

and my htaccess code is:

 RewriteCond %{HTTP_HOST} ^domain_name\.com$ [NC]
 RewriteRule ^(.*)$ http://www.domain_name.com/$1 [R=301]
 RewriteRule ^pages/([^/]*)/$ /pages.php?id=$1 [L]

i was also try with:

RewriteEngine On

# RewriteCond %{QUERY_STRING}   ^id=([0-9]+)$
     # RewriteRule ^pages\.php$   http://www.domain_name.com/pages/%1? [R=301,L]

or

RewriteEngine On
 # Redirectmatch 301 /pages.php?id=11 http://www.domain_name.com/about-us [NC]
      #RewriteRule  /pages.php?id=$1 ^/(.*)$ [R=301,NC,L]

but that was not work.

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.