Forum Moderators: phranque
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]
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
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?
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
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]
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
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)?
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
...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