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:

blah.com/template_group/template/variable1/variable2/variable3/etc...

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

blah.com/account_exec/circa1977/template_group/template

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

My first attempt was with

blah.com/account_exec/circa1977/portal

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:

blah.com/account_exec/circa1977/portal

failed.

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?

Thanks!
Mark
circa1977
 
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]
richardk
 
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 http://www.cocoanet.us/~web8_dantepasquale/blog to http://www.cocoanet.us/dantepasquale/blog -- 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/cocoanet.us/web/dantepasquale/blog -- note the dir /web/ is inserted.

My alias that halfway works is:

Code: Select all
AliasMatch ^/~web8_([^/]+)(/(.*))? /var/www/cocoanet.us/web/$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?
DantePasquale
 
Posts: 6
Joined: Thu Mar 05, 2009 8:47 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 3 guests

cron