Trouble with url Rewrite

Discuss practical ways rearrange URLs using mod_rewrite.

Trouble with url Rewrite

Postby yock » Tue Oct 21, 2008 12:45 pm

Trying to setup a friendly url pagination.

I have this in my htaccess:
Code: Select all
RewriteRule channel/(.*)/page/(.*)/$ videos.php?go=channel&q=&nid=$1&tags=&page=$2


After trying it out live it wasnt working. So I did a rewritelog check and here is what was in there:
Code: Select all
rewrite channel/catname/page/2/ -> /videos.php?go=channel&nid=catname/page/2


When it should rewrite to:
Code: Select all
/videos.php?go=channel&nid=catname&page=2 any suggestions?


From what I'm seeing, its not splitting the values to 1$ and 2$. It just combines them all in one value for 1$ ('catname/page/2').

Any suggestions?
Thanks
yock
 
Posts: 2
Joined: Tue Oct 21, 2008 12:33 pm

Postby laisfun » Wed Oct 22, 2008 12:03 am

RewriteRule ([^/]+)/page/([0-9]{1,2})/$ /videos.php?go=channel&q=&nid=$1&tags=&page=$2 [L]
# you can also try [QSA,L] if the above doesn't work.

This will correctly parse:
channel/catname/page/2/

The above RewriteRule will catch only 1-2 digit numbers for the page
variable. I can't imagine you needing three digit numbers so I only
went to two. It's not necessary to match the channel directory unless
you happen to have other URI's that will also match this sequence.
If that's the case then just add
RewriteRule channel/([^/]+)/page/([0-9]{1,2})/$ ...etc.
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA

Postby richardk » Wed Oct 22, 2008 7:27 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^channel/([^/]+)/page/([0-9]+)/?$ /videos.php?go=channel&nid=$1&page=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby yock » Wed Oct 22, 2008 11:14 am

laisfun: Your solution worked perfectly for me. I heard from someone else last night I was being 'greedy' with the regex I had previously setup. This makes more sense.

richardk: thanks for the input.
yock
 
Posts: 2
Joined: Tue Oct 21, 2008 12:33 pm

Postby laisfun » Wed Oct 22, 2008 11:25 am

I was a bit tired when I responded but looking at Richardk's response, his looks better constructed. I would suggest using his instead.

For performance reasons, if you're also trying to match the channel directory then Richardk's will either pass or fail in the very beginning.

Code: Select all
Options +FollowSymLinks
RewriteEngine On

RewriteRule ^channel/([^/]+)/page/([0-9]+)/?$ /videos.php?go=channel&nid=$1&page=$2 [QSA,L]

# or #
RewriteRule /([^/]+)/page/([0-9]{1,2})/$ /videos.php?go=channel&nid=$1&page=$2 [QSA,L]

# or #
RewriteRule ^channel/([^/]+)/page/([0-9]{1,2})/$ /videos.php?go=channel&nid=$1&page=$2 [QSA,L]



If the ending slash in your URL doesn't matter then the first RewriteRule is better. If you're always going to end your URL's with a slash
e.g., http://www.yourdomain.com/channel/catname/page/2/
then you can use the second or last RewriteRule. I hope this makes sense.[/code]
laisfun
 
Posts: 16
Joined: Sun Aug 27, 2006 7:03 pm
Location: CA, USA


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 19 guests

cron