Link to home
Create AccountLog in
Avatar of Imaginx
Imaginx

asked on

Need a little assistance writing a mod rewrite expression.

Evening Experts.

Looking to write a rewriterule, but i'm no rockstar @ reg exp.

On my laptop, the url is:
http://localhost/~admin/Capo/Spestle/Live/www/?sid=tesams

I want the url to look like
http://localhost/~admin/Capo/Spestle/Live/www/tesams/

The final url will be www.spestle.com, incase that makes a difference in the rule. I imagine it would, it's grouped dif than the url on my local env.

Thanks In Advance Everyone !!
-Steve
Avatar of Imaginx
Imaginx

ASKER

Almost forgot: Here's the current htaccess.

RewriteEngine on
RewriteRule index.php$ index.php?sid=sample

That's just to test that my script is functioning proper ( which it is )
So now I just need to understand the reg exp to put in place of
'index.php' before the $

Thanks again !
Avatar of skullnobrains
RewriteRule ^([^?]*)?sid=([^&]*)(.*) $1?/$2?$3

not 100% sure this is what you want

- will work for any other hostname or path
- will do something weird if there is a filename in the url
- will only work if sid is your first parameter
- will preserve other parameters (and stick an ugly empty first parameter cause i was lazy)
ASKER CERTIFIED SOLUTION
Avatar of Graham N.
Graham N.
Flag of United Arab Emirates image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of Imaginx

ASKER

Thanks skullnobrains & grahamnonweiler ...

Just so I understand the solutions.

1. whats the NC stand for ? if the first rewrite is a success, wont [L] stop it from proceeding to the next line?

2. i believe im understanding this part right, which is what i couldn't overcome. The '?' allows the htaccess to grab everything from the root url AFTER the ?. Which was my problem, because I wanted to use rewrite from the root - but didn't indicate the '?' .. Is that understanding correct ?

3. what do I do if I have a real directory like: /about-us/ .. I imagine that it wouldn't be affected bc the .htaccess doesnt see a ? AND then in the /about-us/ directory, i just change that directories rewrite rules. Correct Assumption ??

Thanks for the help guys, Appreciate the responses. I've read a bunch of reg exp & htaccess stuff - but just haven't put the pieces together successfully.. ( and i suck @ reg exp which makes matters harder )
1) NC means "no case" - in otherwords not case sensitive. The "L" does mean "last" - but for that particular rule. So if there is match then the resulting URL is rewritten. You use the "L" controller here for efficiency, so that where you have to have 2 rewrite rules (which you do in the exampes above) to handle the possible URLs getting passed in by browsers, Apache does not continue executing the subsequent rewrite rules.

2) The ? in the rewrite rules does NOT mean from  "?" in the sent URL. It means from "that" point on. Do not confuse the "?" in this instance with the "Query String". The query string comes after the "?" in the sent URL.

In the rewrite rule ^/? means everything after the domain name in the sent URL.

So -> http://www.domain.com/script.php?some=thing

would result in "script.php" being caught by "^/?"

While, the "some=thing" is stored in the Query String.

3) To handle having actual directories/folders with names that would match a rewrite rule, Apache will look first for directories/folders before executing rewrite rules. However, in that directories/folder you would need to have a .htaccess file switching OFF the rewrite engine. Additionally, you can add conditions to your rewrte rules to stop them from executing on specific files or directories using the -f and -d controllers. In general however, the easiest way is to use directories/folder specific .htaccess files.
i had understood you wanted the reverse transformation.
if you're concerned about other matching stuff, have a look at this

RewriteRule ^(.*)/([^/]*)/*$ $1/?sid=$2

which will take the last folder of an url with no filename or query part and transform it as requested.

this one will also work with an optional filename

RewriteRule ^(.*)/+([^/]*)(/+[^/?]*)$ $1/$3?sid=$2

combine the latter with this one which is the same but works with a query part and adds sid=xxx to the existing query parameters

RewriteRule ^(.*)/+([^/]*)(/+[^/?]*?.*)$ $1/$3&sid=$2

and you end up with these 2 rules which should cover all cases

RewriteRule ^(.*)/+([^/]*)(/+[^/?]*)$ $1/$3?sid=$2
RewriteRule ^(.*)/+([^/]*)(/+[^/?]*?.*)$ $1/$3&sid=$2

Open in new window


it is feasible to write it in a single ereg. just way more complicated

it should look like this but i have not tested it

RewriteRule ^(.*)/+([^/]*)(/+[^/?]*)(?:?(.*)|)$ $1/$3?$4&sid=$2
Avatar of Imaginx

ASKER

Hope you guys enjoyed your holidays.

Skull, you had it right the first time - i was just asking that question about directories bc I knew that the app would have them & I wanted to understand completely.

You both gave me a ton of information to make this proj, a success.
I'm actually workin on the app now, so I'll be putting all this new info to the test :)

I'll award after I complete testing, but I think I have the info needed to succeed.
Thanks again.