Jump to content

Optional Rewrites that can carry data


Xeoncross

Recommended Posts

I was just wondering if I could take some URL like http://site.com/plugin/tumblelog/2/title/date

and turn it into site.com/index.php?page=plugin&type=tumblelog&page=2&data=title/date

 

Now the problem is, I am building this for a system and I wan to make it so that the end stuff (page/title/date) is optional. For example these URL's should also work with the same rule: http://site.com/plugin/tumblelog/, or http://site.com/plugin/tumblelog/5/order/CAP

 

So I have tried this but it doesn't work.

 

Code:
RewriteRule ^plugin/([A-Za-z0-9\-]+)/(.*/)?(.*/)*$ index.php?page=plugin&type=$1&page=$2data=$3 [L]

or 

RewriteRule ^plugin/([A-Za-z0-9\-]+)/(.*/)*$ index.php?page=plugin&type=$1&data=$2 [L]

 

Lets say that the above does work and I pass this URL: http://site.com/plugin/tumblelog/5/order/CAP

Then I was thinking of using some PHP like this to get the data:

<?php
$page_data = explode('/', $_GET['data']);
/*
$page_data[0] = '5';
$page_data[1] = 'order';
$page_data[2] = 'CAP';
*/
?>

 

 

So, can you make parts of the rewrite optional in mod_rewrite? So that I could have (or not have) the end data like "page/title/count" and it wont affect the rewrite?

Link to comment
https://forums.phpfreaks.com/topic/67452-optional-rewrites-that-can-carry-data/
Share on other sites

Ok, that last post was a little confusing  ::)

 

So how about this: I want to know if this code will redirect correctly  :P

 


RewriteRule ^([A-Za-z0-9\-]+)/([A-Za-z0-9\-]+/)?(.*/)?(page_([0-9]+)/)?$ index.php?page=$1&type=$2&data=$3&current_page=$4 [NC,L]

#Examples:
#              page=tag     /    life/         (nodata/)    page_10/
#              page=post    /    blog/         (id/title/)  page_1/
#              page=plugin  /    tumblelog/    (order_by/)  page_3/

 

URL's that would need to be directed:

 

http://site.com/tag/life/page_21 <TO> /index.php?page=tag&type=life&data=&current_page=21

http://site.com/tag/page_1 <TO> /index.php?page=tag&type=&data=&current_page=1

http://site.com/post/articles/45/title_of_post/page_1 <TO> /index.php?page=post&type=articles&data=45/title_of_post?current_page=1

http://site.com/plugin/newsreader/page_5 <TO> /index.php?page=plugin&type=newsreader&data=?current_page=5

 

Is my HTACCESS code right? I guess that after I rewrite the whole system I will know - but I would like to know before hand if there are errors in my logic or code.  ;)

Archived

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

×
×
  • Create New...

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.