Forum Moderators: coopster & phranque

Message Too Old, No Replies

another mod_rewrite question

         

Trisha

11:25 pm on Jul 25, 2002 (gmt 0)

10+ Year Member



This is my first time to use mod_rewrite, I have read through a few tutorials, but still have some questions. Before I ask them, I'll explain more about how the site is set up. The scripting for the site is based somewhat on the content management system example in the Welling and Thomson book, PHP and MySql Web Development. The site currently has 2 sections. The content related to each of them is on it's own table in the db. The files associated with each of them are in their own directories on the server also. The actual text names of the sections and topics are entries in the database also. For each topic, there is a page that will display each article title as a link.

1 - Since each article on the site will be linked to from within the site, is it really even necessary to rewrite the URI's? I know Google would find the pages, but maybe other SE's would too, since they really aren't very long or complex URI's and there is a link to each one on the site. Of course, if they are shorter and simpler it would also be easier for people to remember and/or type them in if they need to.

2 - This site will have URI's of the type:

www.domain.com/section1/article_topic1.php?topic1=article_id
www.domain.com/section1/article_topic2.php?topic2=article_id
www.domain.com/section2/article_topic1.php?topic1=article_id

In the future more sections and topics within those sections will be added. There is a possibility that some topic names will be used in more than one section. The article_id's will be unique within a section, but maybe not between sections.

I would like the URI's to be something like:

www.domain.com/section1/topic1/article_id
or even
www.domain.com/section1/article_id

The only way I can figure out how to do this is to have 3 different rewrite rules:

RewriteRule ^(.*?)section1/topic1/(.*)$
$1section1/article_topic1.php?topic1=$2

RewriteRule ^(.*?)section1/topic2/(.*)$
$1section1/article_topic2.php?topic2=$2

RewriteRule ^(.*?)section2/topic1/(.*)$
$1section2/article_topic1.php?topic1=$2

Is it possible to do this within one rewrite rule?

3 - The code I am now using to generate the links to the articles is:

<a href=\"article_topic1.php?topic1=$topic1[code]\">$topic1[art_title]</a>

If I rewrite the URI's this would need to be changed, but I'm not sure how or if it could be done - wouldn't I have to still pass the variable 'topic1' to the file article_topic1.php? article_topic1.php queries the database by looking for code (which is the article_id) and then pulls out the associated article title, text, etc. from the database.

I apologize if I'm not explaining this clearly. This is my first time to make a site this complex.

jdMorgan

12:56 am on Jul 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Trisha,

Sure, just use a second backreference (Indicated by parens and called by "$")

If I understand what you want to do, then

RewriteRule ^section([0-9])/topic([0-9])/(.*)$ section$1/article_topic$2.php?topic$2=$3

Will take

www.domain.com/section3/topic4/article_14

and rewrite it to:

www.domain.com/section3/article_topic4.php?topic4=article_14

How to decode this mess:

Each pair of parenthesis creates a "back-reference", that is, mod_rewrite "remembers" the
contents that matched what's inside the parens on the left side of the rule.

On the right side, $1, $2, $3, etc. can then be used to sustitute the "remembered" text.

The square brackets are used to specify sets, which can be numbers 0-9, letters a-f or A-F, single
characters - any of which are acceptable, etc. I used 0-9 to match up to 10 sections or topics.

Note that the dollar sign at the end on the *left* side of the RewriteRule is only an "end-of-text"
anchor, not a backreference. Backreferences are only called by "$" on the *right* side of the rule.

Need more? This will accept one- or two-digit sections, and one- to three-digit topics:

RewriteRule ^section([0-9]{1,2})/topic([0-9]{1,3})/(.*)$ section$1/article_topic($2).php?topic$2=$3

The form is {min_count,maxcount} or just {required_count} .

Fun huh?

Well, I hope I understood what you wanted to do! (As always when posting rewrites, I hope I didn't
do a major typo - please test thoroughly before using!!!)

Cheers,
Jim
[edited - Yup... Typo!]

ergophobe

5:38 am on Jul 26, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Trisha,


The actual text names of the sections and topics are entries in the database also. For each topic, there is a page that will display each article title as a link.

That's basically what I do. First the rule then the explanation:

1. RewriteEngine on
2. RewriteRule ^(.*)index.php$ - [L]
3. RewriteRule !(.*)\.(gif¦jpg¦png¦css¦js)$ /index.php [L]

Line 1 should be obvious.
Line 2 stops the thing from getting caught in an infinite loop. Basically it says that if it's already been rewritten, then stop.

Line 3 says, rewrite anything that is not gif/css/etc to index.php

index.php then parses the url that was requested, which has no real meaning - it just corresponds to the path that is created by using the section name + sub section name + sub sub section name + .... + article name (and no, I don't really have this many subsections, but if I did, I would not need to change the code any).

Within index.php it further parses the url and basically checks

1. is it a file that actually exists? If so, just serve that file. This allows me to creat a static page or a zip file or something like that and still serve it without creating a virutal path for it in the DB

2. is it a valid virtual path (ie does it match an article in the DB) to a section index page? If so, serve up that page.

3. is it a valid virutal path to an article? Serve that page

4. it is not a valid page, serve a custom 404 page.

You may find the following page useful (it's basically where I got started):

[phpbuilder.com...]

Also check out the page on A List Apart, which covers pretty much the same thing, but I'd have to search for it.

Tom

Trisha

11:10 pm on Jul 26, 2002 (gmt 0)

10+ Year Member



Thanks for both of your replies! After reading through them once I don't understand either of them, yet, but over the next few days I will go over them again and hopefully will be able to understand your advice.