Jump to content

Archived

This topic is now archived and is closed to further replies.

AdRock

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

Recommended Posts

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.
[code]RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_USER_AGENT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGENT} ^ExtractorPro
RewriteRule ^\.htaccess$ - [F]
RewriteRule ^(.*)/(.*)$ $1index?$2[/code]
here is the some of the links I would like to use

http://www.jackgodfrey.org.uk/index.php
http://www.jackgodfrey.org.uk/index.php?page=contact
http://www.jackgodfrey.org.uk/index.php?page=news&limit=1&pagenum=4

How would i include these links in the .htaccess file? ;D

Share this post


Link to post
Share on other sites
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>

Share this post


Link to post
Share on other sites
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.jackgodfrey.org.uk/index.php?page=news&limit=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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
I have got everything else working but this strange thing happening each time i click a link

/index/index/index/index/index/contact

Share this post


Link to post
Share on other sites
are you linking

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

Share this post


Link to post
Share on other sites
Can you please tell me if I have got this right
[code]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][/code]
and how do i rewrite these urls for mod rewrite?  It for the last line of code where I would get [b]index.php?page=new&limit=1&pagenum=4[/b]

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

Share this post


Link to post
Share on other sites
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])

Share this post


Link to post
Share on other sites
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
[code]<?
//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
?>[/code]

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

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

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>

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.