Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite for 2 rules

First one in list of rules works, stops second from working

         

oceanwave

10:17 pm on Oct 7, 2007 (gmt 0)

10+ Year Member



Hi Everyone,

In my htaccess file is:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^mysite.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.mysite.com$
RewriteRule ^side$ http://site.net/sites/list.php?id=14 [R=301,L]

This was written from cpanel when I re-directed a page mysite.com/side using an addon domain

Now that you know what is in my file, I want to add mod_rewrites for 2 dynamically created pages. Each person submitting a form to the database (receiving a unique userid number) is either classified as a member or a visitor. Members get to use one dynamic page, visitors the other page. No 2 people ever have the same userid number, so if userid #55 is a member, there is no visitor with #55.

Okay, so I wanted to use mod_rewrite to shorten both dynamic pages. This works:

RewriteRule ([^-]*)-([0-9]*).html memberpage.php?userid=$1 [L]

When I added the second rule, only the first one works:

RewriteRule ([^-]*)-([0-9]*).html memberpage.php?userid=$1 [L]
RewriteRule ([^-]*)-([0-9]*).html visitorpage.php?userid=$1 [L]

Did some research and thought I might need to remove the [L]'s to keep it from stopping at the first rule, but that didn't do it. Tried a number of different things, even tried a different set of rules:

RewriteRule ^([^/\.]+)/?$ /memberpage.php?userid=$1 [L]
RewriteRule ^([^/\.]+)/?$ /visitorpage.php?userid=$1 [L]

Only the first rule works. Switching the position of the rules, still only the first one works. I am new to all of this. Would greatly appreciate any help.

[edited by: jdMorgan at 1:17 pm (utc) on Oct. 8, 2007]
[edit reason] de-linked [/edit]

jdMorgan

11:15 pm on Oct 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You must generate a unique "identifier" for each of the two possible pages (user types). Otherwise, with identical patterns, mod_rewrite will always match on the first rule, and ignore the second.

You could use odd/even numbers, a prefix or suffix of some kind, use a hyphen for one type and an underscore for the other -- It doesn't matter, but there must be some difference, and the two RewriteRule patterns must be different. Otherwise, mod_rewrite has no way to 'know' which is which.

Jim

oceanwave

2:22 am on Oct 8, 2007 (gmt 0)

10+ Year Member



Hi jdMorgan,

Thanks for replying and for explaining what the problem was. Tried changing a few things and at one point got a major error message blasted across the screen.

This seems to work:

RewriteRule ([^-]*)-([0-9]*).html memberpage.php?userid=$2 [L]
RewriteRule ^([^/\.]+)/?$ /visitorpage.php?userid=$1 [L]

I had to write $2 and $1 for it to work...don't know why.

Is there a better way to clean up these URL's?

jdMorgan

2:59 am on Oct 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I can't tell, because you've never stated what your URL-paths actually are...

The only useful comment I can make is that it is not necessary to escape literal periods within character [groups] because they, like any other characters in the group, represent a single literal character. The only characters you need to escape are "]" and --if it occurs as the initial group member and you do not wish it to represent a negation operator-- the "^" character, as well as "-" if it might otherwise be interpreted as a range indicator.

So your "[^/\.]+" pattern can be written simply as "[^/.]+"

Jim

oceanwave

3:56 am on Oct 8, 2007 (gmt 0)

10+ Year Member



Hi Jim,

All the files are inside one folder that the addon domain is pointing to. So the addon domain makes the 2 files have the URL format:

http://addondomain.com/memberpage.php?userid=13
http://addondomain.com/visitorpage.php?userid=15

I will be adding other addondomains pointing to the same folder, so I thought I had better come up with a way for the users to remember their URLs.

I spent a number of hours reading posts about mod_rewrite (on Google and searching this forum) looking for examples that were similar to what I wanted to do. That is where I came up with the examples that I posted. I have no idea if what I posted is the best way to do things.

I also read a post warning everyone about using mod_rewrite and it's effect on slowing down the site. Don't know if it is true or not.

Thanks so much for cleaning up the code in my example. Much appreciated!

[edited by: jdMorgan at 1:17 pm (utc) on Oct. 8, 2007]
[edit reason] de-linked [/edit]

jdMorgan

1:05 pm on Oct 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This takes a bit of getting used to, but those are not your URLs, those are now only your filepaths. Introducing mod_rewrite in the URL-to-filename translation phase means that your URLs are no longer 'the same' as the server filepaths used to serve content when those URLs are requested.

From the regex patterns in your RewriteRules, your "member" URL-paths appear to be in the form "/<something>-numbers.html", while any other form is now treated as a "non-member" - but that's all I can tell.

The URLs are defined by what appears in the links on your pages. These are what visitors will see and click on, and these are what search engines will list in their search results. The associated server filepaths are defined by the set-up of your files on your server. Mod_rewrite 'translates' incoming requested URLs to server filepaths.

Jim

oceanwave

8:35 pm on Oct 8, 2007 (gmt 0)

10+ Year Member



Hi jdMorgan,

I had no idea. That one will take me awhile to think about.

You are correct. The member file would be /<anything>-number.html
Anything could be added in the <anything> area and the link will still work.

The visitor fiel would be /number .

I actually think I need to add a third rule for one more file in the same folder, format: lists.php?id=22
Think I need another rule.

Could you please tell me why I needed to use userid=$2 and userid=$1 in the first 2 file rules? Why didn't $1 work for both files (they are separate rules)?

jdMorgan

8:47 pm on Oct 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The number value of $1 and $2 -- The 1 and 2, refer to the value of the URL-path matching the parenthesized sub-patterns. $1 contains the part of the URL-path that matches the first parenthesized sub-pattern, and $2 contains the part of the URL-path that matches the second parenthesized sub-pattern.\

In addition, the "$" refers to the matched value in a RewriteRule subpattern, while a "%" refers to the value matched in a RewriteCond.

These are called "back-references" in the Apache mod_rewrite documentation.

Jim

oceanwave

10:37 pm on Oct 8, 2007 (gmt 0)

10+ Year Member



Thank you so much. I finally understand the difference between $1 and $2. Funny in all the searches I've done, it is only until your explanation that I finally understand when each is used. Thanks again.

jdMorgan

10:51 pm on Oct 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



...in the Apache mod_rewrite documentation [httpd.apache.org]

This is a must-read document. A lot of the stuff you'll find by searching is wrong or very badly coded, or both.

Jim