lt.kraken
lt.kraken

Reputation: 1300

Mod Rewrite on get vars

I'm working on a private project, which is basicly a profile system. Now I've been trying how to improve the URL formatting.

At this moment, I am using URL's which look like:

-> http://example.com/overview
-> http://example.com/profile/?name=foo
-> http://example.com/jobs/?name=foo

Both overview and profile are directories on my website, which contain a single index.php file, which holds the PageID of which should be retrieved from the database.

Now, my goal is to format the URL to something as:

-> http://example.com/foo OR http://example.com/profile/foo
-> http://example.com/foo/jobs OR http://example.com/profile/foo/jobs

Is there anyway to do this with MOD_REWRITE?

This would mean the original url would look something like http://example.com/?character=foo/jobs which is http://example.com/get_var/directory.

I've done my research on Stackoverflow and Google, searching for 'mod_rewrite get variables'. But nothing seemed to be what I'd like to see happening.

Yours Sincerely,
Larssy1

Upvotes: 1

Views: 69

Answers (1)

Michael Berkowski
Michael Berkowski

Reputation: 270785

To extract a portion of the URI between slashes and append it as a URL parameter, use the expression ([^/]+) to capture all characters up to but not including the next / into $1:

RewriteEngine On
RewriteRule ^profile/([^/]+)/([^/]+/?)?$ profile/$2?character=$1 [L]

The above rule will dynamically capture what follows Foo from your example, meaning that whether it was followed by /jobs or /other or /somethingelse, it would be appended as /profile/somethingelse?character=Foo. If that isn't necessary, and /jobs is static, you don't need to capture it into $2:

RewriteEngine On
# Don't dynamically capture what comes after the first variable group...
RewriteRule ^profile/([^/]+)/jobs$ profile/jobs?character=$1 [L]

Upvotes: 1

Related Questions