Mod rewrite question

Hi

I’ve been looking around on the internet and playing around a bit and managed to get this working perfectly:

RewriteRule /sites/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+) /sites/index.php?sid=$4

This takes a url of the form sites/Some-Country/Some-City/Some-Site/siteid and re writes to the dynamic page sites/?sid=siteid

But the next part I’m having some trouble with: I need a url of the form

sites/Some-Country/Some-City/Some-Site/siteid?parameter1=somevalue&parameter2=somevalue

to be re written to the same as earlier but with those extra url parameters. I’ve had no success yet and any help would be appreciated!

Also, while I’m about it, I need what I’ve currently got at /([A-Za-z0-9-]+) to also match foreign characters (such as ã) - how do I do this?

Thanks

ben,

For your first question, the QSA flag is what you’re looking for, i.e.,

RewriteRule /sites/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([0-9]+)[COLOR="#0000FF"]$[/COLOR] /sites/index.php?sid=$4 [QSA,L]

I’ve added the end anchor to fetch the digits from the fourth atom but require them to be the last thing in the URI, the Last flag because it’s important (to mod_rewrite) and the QSA flag to retain any pre-existing query string.

As for your second question (foreigh characters), you’re not needing the $1 - $3 atoms so merely use ([^/]+) for each one. Otherwise, you’ve got to add the accented characters to a character range definition (e.g., [a-zA-Z0-9ã]+ to add your “ã” character). Obviously, more than one character makes this a ridiculous solution to the “NOT /” would be the preferred solution.

Regards,

DK

Thanks so much, works beautifully! Really appreciate the help.

ben,

Which suggestion worked for you?

Okay, neither should work if you’re using Apache 2.x as the leading / in the RewriteRule will be rejected by mod_rewrite. The leading / in the redirection could also be redirected to the sites directory of the server’s root. In other words, if you’re using Apache 2, delete the leading / in the regex; if you’re using either (and the .htaccess is in the DocumentRoot of the domain), delete the leading in the redirection. Sorry I didn’t catch that earlier.

Regards,

DK

I used your first suggestion but I also changed the bits in between the /'s to [^/]+ as, like you said, I don’t need those atoms so this caters for all characters.

I am running Apache 2, and it seemed to work fine! This is on my localhost though where I’ve been testing it and I’ve put it in the httpd.config file. However, I’ve just found its not working on my live site (also running Apache 2), where I put it in the .htaccess file (in the public_html folder). I think its a problem with loading modules though, as when I put

LoadModule rewrite_module modules/mod_rewrite.so

at the top of the file, I get a server error. I’ll ask my host about that though.

ben,

The LoadModule directives are reserved for the httpd.conf (or equivalent). Use the test in my signature’s tutorial article to confirm that mod_rewrite is enabled and properly functioning.

The comment about the leading / in the regex was specifically aimed at Apache 2 installations:

Apache 1.x requires the leading / - ^/
Apache 2.x rejects the leading / - ^
Apache 1.x and 2.x accept - ^/?

The leading / in the redirection is interpreted FIRST as asking for a file relative to the server’s root directory THEN it will look to the DocumentRoot of the domain. Omitting the leading / in the redirection - especially when in the .htaccess in the DocumentRoot - is highly recommended.

Regards,

DK