/id/1 to /index.php?id=1

Discuss practical ways rearrange URLs using mod_rewrite.

Postby ng » Mon Sep 28, 2009 2:58 am

hi,

i need rewrite www.myweb.com/index.php?id=1 (1 is random an image)

to www.myweb.com/id/1

thanks very much richardk for your answer.
ng
 
Posts: 13
Joined: Sat Sep 26, 2009 5:34 pm

Postby richardk » Mon Sep 28, 2009 2:03 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^id/([0-9]+)/?$ /index.php?id=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby ng » Tue Sep 29, 2009 8:16 am

richardk wrote:Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^id/([0-9]+)/?$ /index.php?id=$1 [QSA,L]


does not work :(
ng
 
Posts: 13
Joined: Sat Sep 26, 2009 5:34 pm

Postby richardk » Tue Sep 29, 2009 3:08 pm

What happens when you go to /id/1?
Where did you put the mod_rewrite?
Does this mod_rewrite test work?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby ng » Wed Sep 30, 2009 7:24 am

Forbidden

You don't have permission to access www.mysite.com/id/54361713294754.jpg on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

in root directory

yes its working
ng
 
Posts: 13
Joined: Sat Sep 26, 2009 5:34 pm

Postby richardk » Wed Sep 30, 2009 12:35 pm

Try
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^id/([^/]+)$ /index.php?id=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby ng » Wed Sep 30, 2009 12:44 pm

síí its working te amo richardk unbeso :D
ng
 
Posts: 13
Joined: Sat Sep 26, 2009 5:34 pm

help me url

Postby xiketuan » Sun Oct 18, 2009 2:50 am

I use PHPNuke, the url rewrite is domain/folder/cattile/title.html friendly format, the homepage is running very well, but in the admin access, then all paths are wrong, for example admin.php file in the directory admin, but accessing the link modules in the admin folder omitted admin
link : domain/folder/admin/admin.php?op=modules but when i use rewrite with / then : domain/folder/admin.php?op=modules

help me.
xiketuan
 
Posts: 10
Joined: Wed Feb 25, 2009 4:56 pm

Postby richardk » Mon Oct 19, 2009 11:10 am

Where did you get the mod_rewrite from?
What is the mod_rewrite?
What is the HTML link (<a href="...">) like in the source code of the page?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby xiketuan » Mon Oct 19, 2009 7:24 pm

file .htaccess :
Code: Select all
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([a-zA-Z0-9-_]*)/([1-9][0-9]*)-([a-zA-Z0-9-_]*).* modules.php?name=News&op=viewst&catseo=$1&sid=$2&titleseo=$3 [L]
RewriteRule ^([0-9]*)/([a-zA-Z0-9-_]*).html modules.php?name=News&op=viewcat&catid=$1&catseo=$2 [L]
RewriteRule ^([0-9]*)/([a-zA-Z0-9-_]*).html modules.php?name=News&op=viewcat&catid=$1&catseo=$2 [L]

file GT-news.php :

Code: Select all
$urlin = array(
'"(?<!/)modules.php\?name=News&amp;op=viewst&amp;catseo=([a-zA-Z0-9-_]*)&amp;sid=([0-9]*)&amp;titleseo=([a-zA-Z0-9-_]*)"',
'"(?<!/)modules.php\?name=News&amp;op=viewcat&amp;catid=([0-9]*)&amp;catseo=([a-zA-Z0-9-_]*)"'
);

$urlout = array(
'\\1/\\2-\\3.html',
'\\1/\\2.html',);




file shortlink :
Code: Select all
<?php

if (!defined('TLNN_USE_SHORTLINKS')) {
   die('Access Denied');
}
/*
* Configuration settings for ShortLinks
*/

$tlnn_sGTFileDir = 'ShortLinks';    // Name of Directory for GoogleTap Files. Should be located in root path with mainfile.php.
$tlnn_asGTFileExclude = array(      // This array is only used while in debug mode if you don't want the admin to be bothered with the "untapped" notices below the footer
   'Resend_Email',                 // This module should only be active for Administrators, so tapping it is unnecessary
   'NukeSentinel',                 // One could tap this, but what is the point?  Should probably only be available to admins anyways
   'UserInfoAddons',               // This module should only be active for Administrators, so tapping it is unnecessary
   'block-Sentinel_Center.php',    // Has no links, so why attempt to tap
   'block-Sentinel_Scrolling.php', // Has no links, so why attempt to tap
   'block-Sentinel_Side.php'       // Has no links, so why attempt to tap, etc...
   );
/*
* End of Configuration Options
*/

$tlnn_asGTFilePath = array(); // <-- Do NOT Touch!
$tlnn_asNoGTFile = array(); // <-- Do NOT Touch!
$tlnn_bDebugMode = (defined('NV_ADMIN') && isset($tlnn_bDebugShortLinks) && $tlnn_bDebugShortLinks) ? true : false; // <-- Do NOT Touch!

/**
* Function: tlnn_fPageTapStart
*
* Initiates the page tap for bufferring the output
*
* @return array of GT file paths if any
*/
function tlnn_fPageTapStart() {
   global $prefix, $db, $admin, $tlnn_bDebugMode, $tlnn_sGTFileDir, $tlnn_bAutoTapLinks, $tlnn_asNoGTFile;
   /*
        * Modified by montego & xiketuan from http:// tanhlinh.com for ShortLinks
    */
   $asGTFilePath = array();
   $sGTFilePath = '';
   if ($tlnn_bDebugMode) {
      echo '[ShortLinks_Function] = Present In Header<br />';
   }
   if (isset($_REQUEST['name']) && $_REQUEST['name'] != '') {
      $nextGenName = htmlentities(stripslashes($_REQUEST['name']));
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_ModuleName] = ' . $nextGenName . '<br />';
      }
   }
   else
   { $nextGenName = "News";}
   /*else {
      $sql = 'SELECT main_module FROM ' . $prefix . '_main';
      $result = $db->sql_query($sql);
      $row = $db->sql_fetchrow($result);
      $mainmod_name = $row['main_module'];
      $nextGenName = $mainmod_name;
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_ModuleName] = ' . $nextGenName . ' (Main Module)<br />';
      }
   }*/
   /*
    * GT-NExtGEn Files Path --- nextGenPath Variable
   
    */
   $sGTFilePath = $tlnn_sGTFileDir . '/GT-' . $nextGenName . '.php';
   if (@file_exists($sGTFilePath)) {
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_FilePath] = GT File :: ' . $sGTFilePath . ' :: Does Exist<br />';
      }
      ob_start();
      array_push($asGTFilePath, $sGTFilePath);
      define('TLNN_BUFFERED', TRUE);
   } else {
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_FilePath] = GT File :: ' . $sGTFilePath . ' :: Does Not Exist!<br />';
      }
      $tlnn_asNoGTFile[] = $sGTFilePath;
   }
   /*
    * Check for PageLevel tap file
    */
   $sGTFilePath = $tlnn_sGTFileDir . '/GTZ-PageTap.php';
   if (@file_exists($sGTFilePath)) {
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_FilePath] = GT File :: ' . $sGTFilePath . ' :: Does Exist<br />';
      }
      if (!defined('TLNN_BUFFERED')) { // Only buffer the output if it has not already been done
         ob_start();
         define('TLNN_BUFFERED', TRUE);
      }
      array_push($asGTFilePath, $sGTFilePath);
   } else {
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_FilePath] = GT File :: ' . $sGTFilePath . ' :: Does Not Exist!<br />';
      }
      $tlnn_asNoGTFile[] = $sGTFilePath;
   }
   /*
    * If none of the above produces a tap, need to check if debug is on AND if tlnn_bAutoTapLinks is TRUE
    */
   if (!defined('TLNN_BUFFERED') && $tlnn_bDebugMode && isset($tlnn_bAutoTapLinks) && $tlnn_bAutoTapLinks) {
      ob_start();
      define('TLNN_BUFFERED', TRUE);
   }
   return $asGTFilePath;
}

/**
* Function: tlnn_fPageTapFinish
*
* Completes the page tap by taking the bufferred output and running through
* the various ShortLinks files (GT-<<module name>>.php).
*
* @return void does not return anything, but does output the final HTML
*/
function tlnn_fPageTapFinish() {
   global $tlnn_bDebugMode, $tlnn_bAutoTapLinks, $tlnn_sGTFileDir, $tlnn_asNoGTFile, $tlnn_asGTFileExclude;
   $asGTFilePathTmp = $GLOBALS['tlnn_asGTFilePath'];
   if (defined('TLNN_BUFFERED')) {
      $nextGenContent = ob_get_contents();
      tlnn_fCleanLinks($nextGenContent);
      ob_clean();
   } else {
      return;
   }
   /*
    * GT-NExtGEn Footer Code --- nextGenFoot Variable
    */
   if ($tlnn_bDebugMode) {
      echo '<br />[ShortLinks_Function] = Present In Footer<br />';
   }
   /*
    * GT-NExtGEn Block Code --- nextGenBlock Variable
    * Modified by montego & xiketuan ShortLinks
    * I moved this code out of the blocks, since I already have GTB tap file feature there.  This, if enabled,
    * will inspect ALL other non-tapped modules.php type links and either attempt to find a GT file to use,
    * or report back any remaining un-tapped links to the admin if in debug mode.
    */
   if (isset($tlnn_bAutoTapLinks) && $tlnn_bAutoTapLinks) {
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_Function] = Present in AutoTap Links Code<br />';
      }
      if (preg_match_all('(\b[name]{4}=[a-zA-Z0-9_-]+)', $nextGenContent, $asGotGenMatches)) { // Had links to modules
         $sGTFilePath = '';
         $asNoGTFile = array();
         $asNewGotGenMatches = array_unique($asGotGenMatches[0]);
         foreach($asNewGotGenMatches as $newGenMatch) {
            $expGenMatch = explode('=', $newGenMatch);
            $sGTFilePath = $tlnn_sGTFileDir . '/GT-' . $expGenMatch[1] . '.php';
            if ($tlnn_bDebugMode) {
               echo '[ShortLinks_TapLinks] = Found Links For Module: ' . $expGenMatch[1] . '<br />';
            }
            if (file_exists($sGTFilePath)) {
               array_push($asGTFilePathTmp, $sGTFilePath);
            } else {
               $tlnn_asNoGTFile[] = $sGTFilePath;
            }
            unset($expGenMatch);
         }
      }
   }
   /*
    * Taps are needed, so run through the ShortLinks GT files
    */
   if (count($asGTFilePathTmp) > 0) {
      $asGTFilePath = array_unique($asGTFilePathTmp);
      foreach($asGTFilePath as $sGTFilePath) {
         if ($sGTFilePath != '') {
            if ($tlnn_bDebugMode) {
               echo '[ShortLinks_PerformTap] = Using GT File :: ' . $sGTFilePath . '<br />';
            }
            unset($urlin, $urlout);
            include($sGTFilePath);
            $nextGenContent = preg_replace($urlin, $urlout, $nextGenContent);
         } else {
            if ($tlnn_bDebugMode) {
               echo '[ShortLinks_PerformTap] = GT File :: ' . $sGTFilePath . ' :: Does Not Exist!<br />';
            }
         }
      }
   }
   /*
    * If there are still untapped links, if in debug mode, show the admin what modules are left which need GT/GTB files:
    */
   if ($tlnn_bDebugMode && count($tlnn_asNoGTFile) > 0) {
      foreach($tlnn_asNoGTFile as $sNoGTFile) {
         if (!in_array($sNoGTFile, $tlnn_asGTFileExclude)) {
            echo '[ShortLinks_UnShortenedLinks] = Consider creating GT/GTB File(s) :: ' . $sNoGTFile . '<br />';
         }
      }
   }
      $sFinishUpContent = ob_get_contents();
   ob_end_clean();
   echo $nextGenContent;
   echo $sFinishUpContent;
   return;
}

/**
* Function: tlnn_fShortenBlockURLs
*
* Takes the $content from an individual block (called from mainfile.php) and
* shortens the links using its corresponing GTB-<<block file name>>.php if one exists.
*
* @param  string $sBlockfile is the name of the block file that is needing to be "tapped"
* @param  string $sContents is the block HTML contents to have links shortened
* @return string the contents of the block with links shortened
*/
function tlnn_fShortenBlockURLs($sBlockfile = '', $sContents = '') {
   global $tlnn_bDebugMode, $tlnn_sGTFileDir, $tlnn_asNoGTFile;
   
      if ($tlnn_bDebugMode) {
         echo '[ShortLinks_Function] = Present In Block<br />';
      }
      if ($sBlockfile != '') {
         $sGTBlockPath = $tlnn_sGTFileDir . '/GTB-' . $sBlockfile;
         if (@file_exists($sGTBlockPath)) { // Block has been "Tapped"
            if ($tlnn_bDebugMode) {
               echo '[ShortLinks_FilePath] = GTB File Exists for :: ' . $sBlockfile . '<br />';
            }
            tlnn_fCleanLinks($sContents);
            unset($urlin, $urlout);
            include_once($sGTBlockPath);
            $sContents = preg_replace($urlin, $urlout, $sContents);
         } else {
            if ($tlnn_bDebugMode) {
               echo '[ShortLinks_FilePath] = GTB File Does NOT Exist for :: ' . $sBlockfile . '<br />';
               $tlnn_asNoGTFile[] = $sGTBlockPath;
            }
         }
      } else {
         if ($tlnn_bDebugMode) {
            echo '[ShortLinks_Function] = In HTML Block<br />';
         }
      }
   
   return $sContents;
}

/**
* Function: tlnn_fCleanLinks
*
* In order to find the necessary link patterns, ALL URLs must have a consistent usage
* of the "&", namely "&amp;".
*
* $param string &$getNextGen is passed by reference for speed.  This is for the string
*                 of HTML coming into the function.
* @return void the return is the variable passed by reference
*/
function tlnn_fCleanLinks(&$getNextGen) {
   $getNextGen = preg_replace('(&(?!([a-zA-Z]{2,6}|[0-9\#]{1,6})[\;]))', '&amp;', $getNextGen);
   $getNextGen = str_replace(array(
      '&amp;&amp;',
      '&amp;middot;',
      '&amp;nbsp;',
      '&amp;#'
   ), array(
      '&&',
      '&middot;',
      '&nbsp;',
      '&#'
   ), $getNextGen);
   return;
}
?>
xiketuan
 
Posts: 10
Joined: Wed Feb 25, 2009 4:56 pm

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 34 guests

cron