PDA

View Full Version : Regular Expression


Jon W
12-02-2009, 08:41 AM
Hello, I was wondering if I could get help making a Regular Expression for my server rewrite.

I currently got a rewrite for one for one of my pages and I was just finishing up on my user account home page and I am ready for the other user pages but how the rewrite works is its:

http://example.com/user/username


Now I was playing around with some idea and I was thinking about making the URL something like this:

http://example.com/user/username.messages

OR

http://example.com/user/username/messages


Can someone help me with both of those rewrites?

The dynamic URL would be:

http://example.com/index.php?user=someusername&view=some_category

My current rewrite is like this


RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?user=$1



Any help on this that would be wonderful.


Thanks,
Jon W

Jon W
12-02-2009, 10:50 AM
Can anyone help me out?

Jon W
12-02-2009, 12:39 PM
Well, I got something close to what I was trying to do. Only its kinda of annoying because with this rewrite you can't leave out the "," in the URL. So in other words:

http://example.com/user/jon,

If you were to just put:

http://example.com/user/jon

It would take you to page not found. :(

How can I change this so it wont require the "," in the URL?

Here's the rewrite


RewriteEngine On
RewriteRule (.*),(.*) index.php?user=$1&view=$2

Inigoesdr
12-02-2009, 02:55 PM
How can I change this so it wont require the "," in the URL?
Cascade them:

RewriteEngine On
RewriteRule (.*),(.*) index.php?user=$1&view=$2 [L]
RewriteRule (.*) index.php?user=$1 [L]

If you want to use period(.) instead of a comma(,) like you had in the OP just escape it with a forward slash: \.

Jon W
12-02-2009, 03:10 PM
The code that you just posted still doesn't work. I'm unable to get the value from either one of them. :(

But it does the rewrite just fine. Kinda odd to me that I can't get the value from them.

Jon W
12-02-2009, 03:49 PM
I got this rewrite to work with a little playing around with it. Here's the final rewrite.


RewriteEngine On
RewriteRule (.*),(.*) index.php?user=$1&view=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)(/|)$ index.php?user=$1 [L]


Thanks,
Jon W