Search
Left Quote    Nearly all men can stand adversity, but if you want to test a man's character, give him power.
- Abraham Lincoln    
Right Quote
 
[login] | [Register]
Forum Index » PHP and mySQL » Viewing Topic and Replies
Post Reply


        Viewing Topic: Mod rewrite
  This user is offline  AdRock
  Subject: "Mod rewrite" Posted: @ 2:52 pm on Aug 20 2006   
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I am trying to do mod rewrite but am having trouble

Here is the code for my .htaccess file

Code:
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_USER_AGE NT} ^EmailSiphon [OR]
RewriteCond %{HTTP_USER_AGE NT} ^ExtractorPro RewriteRule ^\.htaccess$ - [F]

RewriteRule ^index/([A-Za-z _0-9]*) index.php?page= $1 [L]

The last line of code I got from another source which does work until I use another hyperlink.
If I click on contact about 5 times i get this

index/index/ind ex/index/index/ contact

I am trying to mod rewrite these URLs

index.php?page= contact
index.php?page= news&limit= 1&page=4 etc


    Viewed: 25,537 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Mod rewrite" Posted: @ 5:10 pm on Aug 20 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

Are you trying to mod rewrite index.php?page= contact to index/contact ? and index.php?page= news&limit= 1&page=4 to
index/news/1/4 ?




-bs0d | AllSyntax.com

    Viewed: 25,529 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: Mod rewrite" Posted: @ 6:21 pm on Aug 20 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

yes....exactly what i'm trying to do




    Viewed: 25,526 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Mod rewrite" Posted: @ 9:18 pm on Aug 20 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

try:


Code:


RewriteEngine on
RewriteBase /
RewriteRule ^(.*)/ index.php?page= $1 [NC]
RewriteRule ^(.*)/(.*)/(.*) / index.php?page= $1&limit=$2 &page=$3 [NC]



something like that...




-bs0d | AllSyntax.com

    Viewed: 25,522 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: Mod rewrite" Posted: @ 10:07 pm on Aug 20 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

That didn't work. Every time i clicked a link it would default to the home page (home.php)

This worked but i couldn't do any pagination
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index/(.*) index.php?page= $1 [L]

I need to add a rewrite rule like this to do the pagination

has it got anything to do with this

switch ($_GET['page' ])
{

case "change-p assword":
include('chan ge-password.php ');
break;

case "contact ":
include('cont act.php');
break;

default:
include('home .php');
}



Edited at 02:49:26 pm on 08/21/06


    Viewed: 25,519 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: Mod rewrite" Posted: @ 12:43 am on Aug 22 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

Is this correct for mod rewrite

Options +FollowSymLinks
RewriteEngine on
RewriteBase /

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


Can you tell me how you would rewrite these urls used in your pagination script becuase as i understand it each of the links within the pages has to be in the same format such as http://www.y oursite.com/stu ff/php/10/1.php

e.g.

Code:

$prev_page = $page - 1;

if($prev_page >= 1) {
echo("< ;b><<& lt;/b> <a href=http://www .yoursite.com/s tuff/script.php ?cat=$cat&l imit=$limit& ;page=$prev_pag e><b>P rev.</b>& lt;/a>" ;
}




    Viewed: 25,504 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Mod rewrite" Posted: @ 3:42 am on Aug 22 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

What you're doing with Mod_Rewrite is making the URL prettier, basically. Your script still needs the same parameters, and they will still be passed, except they will be viewed differently in the URL. The script can still access them the same way. if you have:

index.php?page= 1&limit=2 , then you can still access $_GET['page'] and $_GET['limit' ].

So if you check out the rewrite rules, each "wild-car d" or (.*) will be referenced to $1, $2 and so on respectively.

So in my pagination this is basically the code I use for displaying available code samples (cat_list.php) and then viewing code samples (view.php):


Code:


RewriteEngine on
RewriteBase /code/
RewriteRule ^(.*)/(.*)/(.*) \.php$ cat_list.php?ty pe=$1&limit =$2&page=$3 [NC]
RewriteRule ^(.*)/(.*)/(.*) / view.php?id=$2 [NC]



Now apply that same concept to your own files/directori es. Any further questions on the coding for it, i'd do a google search on regex



Edited at 03:52:16 am on 08/22/06


-bs0d | AllSyntax.com

    Viewed: 25,498 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: Mod rewrite" Posted: @ 11:57 pm on Aug 22 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I got the mod rewrite working properly now. Everywhere I went said different things but I have one last question

I have 3 pages called jack1, jack2, jack3 and jack4.

How would i rewrite it so it reads /jack/1 etc or is it not possible




    Viewed: 25,490 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Mod rewrite" Posted: @ 1:33 am on Aug 23 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

well... im sure its possible. I dont know the answer off the top of my head. But I will tell you one thing, the directories of Mod_Rewrite only need to mach the ID's from the query string. The others (like title of article) can be anything. you could visit:

/tutorials/php/ 15/this_tutoria l_dont_exist/1. php and its going to load the tutorial with the id of 15 and page 1, because the title does not matter. You could put anything.

So you could do the same thing with Jack




-bs0d | AllSyntax.com

    Viewed: 25,487 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: Mod rewrite" Posted: @ 12:52 pm on Aug 31 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I got a weird problem with using $_GET['id'] to get the id from a rewritten url. I have created a simple image gallery but when i click on a thumbnail, the page with the enlarged image doesn't display an image.

Here is my code which is causing the problem

Code:

<?php

include_once(& quot;includes/c onnection.php& quot;

$id = $_GET['id'];< br />
$query = "SELECT * FROM images WHERE id = '$id'";
$result = mysql_query($qu ery) or die(mysql_error ());

while ($row = mysql_fetch_arr ay($result)) {

echo "<img src='/gallery/ ".$row[' image']." ;'>";
}

?>



The url looks like this

http://www.jac kgodfrey.org.uk /image/1

When i click on an image it opens the above page code which is supposed to open the enlarged image but it doesn't

I think it's to do with the $_GET becuase if i don't mod rewrite the url it works




    Viewed: 25,437 Times | Reply to This | To top
  This user is offline  misterhaan
  Subject: "re: Mod rewrite" Posted: @ 3:41 pm on Aug 31 2006    

Member #: 5
Rank: User - (213)
Since: 02/11/05
Posts: 148
From: chair

what does your rewrite look like, and what exactly isn't working for you?




please note that the above post is likely made up in its entirety.

    Viewed: 25,432 Times | Reply to This | To top
  This user is online  Guest
  Subject: "re: Mod rewrite" Posted: @ 6:32 pm on Aug 31 2006    
Unregistered

make sure it has a .php extension in your Rewrite rule?



    Viewed: 25,429 Times | Reply to This | To top
  This user is offline  AdRock
  Subject: "re: Mod rewrite" Posted: @ 12:15 am on Sep 01 2006    
Member #: 186
Rank: User - (31)
Since: 07/21/06
Posts: 31
From: Devon

I found out what the problem is.....there is a conflict between 2 rewrite rules.

The first rewrite rulle will always work but not the second even if i switch them around.

I need them both to work

RewriteRule ^([A-Za-z0-9]+) /([0-9]+)/?$ index.php?page= $1&id=$2
RewriteRule ^([A-Za-z0-9]+) /([0-9]+)/?$ index.php?page= $1&pagenum= $2




    Viewed: 25,424 Times | Reply to This | To top
  This user is offline  bs0d
  Subject: "re: Mod rewrite" Posted: @ 12:36 am on Sep 01 2006    

Member #: 1
Rank: Admin. - (1,505)
Since: 02/06/05
Posts: 600
From: USA

why are you listing page twice??

?page=1&pag enum=2 ... it dont work like that, you cant have 2 rules for the same directories. If you want the 2nd, make your url's like this:

index.php?page= $1&id=$2&am p;pagenum=$3

RewriteRule ^([A-Za-z0-9]+) /([0-9]+)/([0-9 ]+)?$ index.php?page= $1&id=$2pag enum=$3



Edited at 12:40:12 am on 09/01/06


-bs0d | AllSyntax.com

    Viewed: 25,421 Times | Reply to This | To top
Viewing Page: 1 of 1


1 |

This Topic is Locked






"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank