Jump to content


Photo

How the hell do I do mod rewrite? [RESOLVED]


  • Please log in to reply
8 replies to this topic

#1 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 19 August 2006 - 11:43 PM

I am trying to do mod rewrite for my website to make the urls user and search engine firendly and am having trouble getting it to work.  This is what I've got and it doesn't work.
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^(.*)/(.*)$ $1index?$2
here is the some of the links I would like to use

http://www.jackgodfr...rg.uk/index.php
http://www.jackgodfr...hp?page=contact
http://www.jackgodfr...mit=1&pagenum=4

How would i include these links in the .htaccess file? ;D
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#2 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 20 August 2006 - 11:44 AM

ok
Try this in your HTACCECSS

RewriteEngine On
Options +FollowSymlinks
RewriteRule ^index/([A-Za-z_0-9]*) index.php?page=$1 [L]

Then on your website link to them as follows
<a href='index/contact'>Contact</a>
Tell me the problem, I will try tell you the solution

#3 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 20 August 2006 - 02:04 PM

Thanks...that works

Is there a way i can change the index to something else so it reads something/contact etc
Also who do I rewrite this url

http://www.jackgodfr...mit=1&pagenum=4

Many thanks for your help

I have found something strange and I don't think it's supposed to be happening.
If I click on the contact link multiple times even if I one from a different link i get this url

/index/index/index/contact and it doesn't display properly
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#4 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 20 August 2006 - 06:21 PM

yes

Rewrite Rule LinkyouWant, ActualLink [L]
using the ([A-Za-z_0-9]*) it checks for something that is Capital Letters, small letters and numbers only
Tell me the problem, I will try tell you the solution

#5 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 21 August 2006 - 12:02 AM

I have got everything else working but this strange thing happening each time i click a link

/index/index/index/index/index/contact
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#6 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 21 August 2006 - 12:04 AM

are you linking

<a href='/index/contact'>Contact</a>
or
<a href='index/contact'>Contact</a>
The top one is correct

Tell me the problem, I will try tell you the solution

#7 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 21 August 2006 - 12:11 PM

Can you please tell me if I have got this right
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro
RewriteRule ^\.htaccess$ - [F]

RewriteRule ^index/([A-Za-z_0-9]*) index.php?page=$1
[b]RewriteRule ^index/([A-Za-z_0-9]*)/([A-Za-z_0-9]*)/([A-Za-z_0-9]*) index.php?page=$1&limit=$2[/b]$pagenum=$3 [L]
and how do i rewrite these urls for mod rewrite?  It for the last line of code where I would get index.php?page=new&limit=1&pagenum=4

index.php?page=news&limit=$limit&amp;pagenum=$prev_page
index.php?page=news&limit=$limit&amp;pagenum=$a

This is in my pagination script I downloaded and the strings in there are set variables
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still

#8 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 21 August 2006 - 12:52 PM

All you need is
RewriteEngine on
Options +FollowSymlinks

RewrireRule ^index/([a-z]*)/([0-9]*)/([0-9*) index.php?page=$1i&new=$2$pagenum=$3 [L]

(remember the flag at the end [L])
Tell me the problem, I will try tell you the solution

#9 AdRock

AdRock
  • Members
  • PipPipPip
  • Advanced Member
  • 911 posts

Posted 22 August 2006 - 12:06 PM

That makes sense now but how do i change the actual urls in the page themselves?  I have it all working so the mod rewrite works but for the second rewrite rule.  I can't tell if it works becuase of the links on the page

<a href=index.php?page=news&limit=$limit&amp;pagenum=$prev_page>Prev.</a>

<a href=index.php?page=news&limit=$limit&amp;pagenum=$a> $a </a>

<a href=index.php?page=news&limit=$limit&amp;pagenum=$next_page><b>Next</b></a>

Here is my script so you can see how it works
<? 
//REMEMBER TO CONNECT TO DATABASE! 

include_once("includes/connection.php");
    @mysql_connect($host, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER"); 
    @mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB"); 
//**EDIT TO YOUR TABLE NAME, ECT. 

$t = mysql_query("SELECT * FROM `news`"); 
  if(!$t) die(mysql_error()); 
    
$a                = mysql_fetch_object($t); 
$total_items      = mysql_num_rows($t); 
$limit            = $_GET['limit']; 
$type             = $_GET['type']; 
$page             = $_GET['pagenum']; 

//set default if: $limit is empty, non numerical, less than 1, greater than 50 
if((!$limit)  || (is_numeric($limit) == false) || ($limit < 2) || ($limit > 50)) { 
     $limit = 1; //default 
} 
//set default if: $page is empty, non numerical, less than zero, greater than total available 
if((!$page) || (is_numeric($page) == false) || ($page < 0) || ($page > $total_items)) { 
      $page = 1; //default 
} 

//calcuate total pages 
$total_pages     = ceil($total_items / $limit); 
$set_limit          = $page * $limit - ($limit); 

//query: **EDIT TO YOUR TABLE NAME, ECT. 

$q = mysql_query("SELECT id,title,content, DATE_FORMAT(time, '%W %D %M %Y') as date, photo FROM news ORDER BY time DESC LIMIT $set_limit, $limit"); 
  if(!$q) die(mysql_error()); 
     $err = mysql_num_rows($q); 
       if($err == 0) die("No matches met your criteria."); 
$numofrows = mysql_num_rows($q);
//Results per page: **EDIT LINK PATH** 
echo("   
<a href=?page=news&limit=10&amp;pagenum=1></a> 
<a href=?page=news&limit=25&amp;pagenum=1></a> 
<a href=?page=news&limit=50&amp;pagenum=1></a>"); 

//show data matching query: 
while($code = mysql_fetch_array($q)) { 

    echo "<div id=\"right\"><h3>".$code['title']."</h3><i>Date added: ".$code['date']."</i><p class=\"style3\"><img src=images/".$code['photo']." align=\"right\" class=\"right\">".nl2br($code['content'])."</p></div>\n";
} 

$id = urlencode($id); //makes browser friendly 

//prev. page: **EDIT LINK PATH** 

$prev_page = $page - 1; 

if($prev_page >= 1) { 
  echo("<b>&lt;&lt;</b> <a href=?page=news&limit=$limit&amp;pagenum=$prev_page><b>Prev.</b></a>"); 
} 

//Display middle pages: **EDIT LINK PATH** 

for($a = 1; $a <= $total_pages; $a++) 
{ 
   if($a == $page) { 
      echo("<b> $a</b> | "); //no link 
     } else { 
  echo("  <a href=?page=news&limit=$limit&amp;pagenum=$a> $a </a> | "); 
     } 
} 

//next page: **EDIT THIS LINK PATH** 

$next_page = $page + 1; 
if($next_page <= $total_pages) { 
   echo("<a href=?page=news&limit=$limit&amp;pagenum=$next_page><b>Next</b></a> &gt; &gt;"); 
} 

//all done 
?>

Here is my updated .htaccess mod rewrite
Options +FollowSymLinks
RewriteEngine on
RewriteBase /

RewriteRule ^honeylands/(.*) index.php?page=$1 [NC]
RewriteRule ^honeylands/(.*)/(.*)/(.*) index.php?page=$1&limit=$2&pagenum=$3 [L]

Is this right for the internal urls? It seems to display correctly in the browser address bar but it defualts back to the home page which is now /honeylands/index

<a href=/honeylands/news/$limit/$prev_page><b>Prev.</a>
<a href=/honeylands/news/$limit/$next_page><b>Next</a>
<a href=/honeylands/news/$limit/$a> $a </a>
If your topic has been solved, please mark the topic as SOLVED.

This helps others from identifying which topics need help still




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users