Trying to rearrange URL segments

Discuss practical ways rearrange URLs using mod_rewrite.

Trying to rearrange URL segments

Postby circa1977 » Sat Aug 08, 2009 6:25 pm

I'm working with a CMS that reads URLs as:

I'm looking to create the perception that sites are grouped under an account exec and a customer/prospect. For example:

So I need to consistently bounce segments 3 & 4 to 1 & 2 in the rewritten URL.

My first attempt was with

This worked for that:

Code: Select all
RewriteRule ^(.*)/(.*)/(.*)$ /index.php/$3/$1/$2 [L]

The rewritten URL was read as portal/account_exec/circa1977, using the portal template and treating the other two values as variables I can filter content on.

Then I wanted to add a variable to the end, so I updated it to:

Code: Select all
RewriteRule ^(.*)/(.*)/(.*)/(.*)$ /index.php/$3/$1/$2/$4 [L]

But then requests for:


I can do some work with subdirectories on the server to set up different htaccess with different rulesets, but how do I consistently, selectively take, for example, segment 3 and move it to segment 1 regardless of how many segments are on the URL?

Posts: 1
Joined: Sat Aug 08, 2009 6:19 pm

Postby richardk » Wed Aug 12, 2009 10:54 am

Use two rules.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$         /index.php/$3/$1/$2    [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php/$3/$1/$2/$4 [L]
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby DantePasquale » Mon Nov 16, 2009 12:07 pm

I'm trying to do something similar but need to actually drop some characters off of one of the segments. I have it halfway working via an ALIAS directive, but I'm pretty sure I want a re-direct instead of an ALIAS.

I need to change older style URL of form to -- should be simple! The key is if the request has the '~web8_', then change to point to the proper doc root and subdirectory, which is /var/www/ -- note the dir /web/ is inserted.

My alias that halfway works is:

Code: Select all
AliasMatch ^/~web8_([^/]+)(/(.*))? /var/www/$1/$3

How could I change this to do a redirect instead of the alias? And, should I rewrite to the new URL and not worry about the doc root since that's handled elsewhere?
Posts: 6
Joined: Thu Mar 05, 2009 8:47 am

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 8 guests