/name1-name2-...-nameN.php -> /name1/name2/.../nameN

Discuss practical ways rearrange URLs using mod_rewrite.

/name1-name2-...-nameN.php -> /name1/name2/.../nameN

Postby nikopol » Tue Jun 20, 2006 11:29 pm

Hello,

My current URLS look like:
1. /about.php
2. /about-extras.php
3. /about-extras-something1.php
...
4. /solutions-something2.php
...

So I have unknown number of dashes.
I would like to have standard:

1. /about (/about/)
2. /about/extras (/about/extras/)
3. /about/extras/something1 (/about/extras/something1/)
...
4. /solutions/something2 (solutions/something2/)
...

So I would like each "-" to be rewritten into "/" and additional ending dash which can but musn't appear.

Can you give me some clue, how to do it using mod_rewrite?

Cheers,
nikopol
 
Posts: 1
Joined: Tue Jun 20, 2006 11:19 pm

Postby richardk » Wed Jun 21, 2006 2:44 am

Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

# replace all "/"s with a "-",
# ignore the optional final "/"
RewriteRule ^(.*)/(.*)([^/])/?$ $1-$2$3 [E=QR:$1-$2$3]

# when they're all gone
RewriteCond %{ENV:QR} .
RewriteCond %{ENV:QR} !/
# append .php and request the file.
RewriteRule .* /%{ENV:QR}.php [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 37 guests

cron