Help with a specific rewrite

Using mod_rewrite to handle various content issues

Help with a specific rewrite

Postby desdev » Thu Nov 01, 2007 8:01 am

I would like to restructure all incoming requests as follows. All the links in my site are structured like:

/dogs/monkies/chickens/
/dogs/
/ (home page)


These links may or may not have the trailing slash and the depth of the link ( as I show above) is not always the same, ie there may be a link to /dogs/monkies/chickens/goats/farmers/ (that's 5 levels).

I would like to redirect those requests to reflect the file structure, which is:

/_pages/dogs/monkies_chickens.php or
/_pages/dogs/dogs.php

so a request for /dogs/monkies/chickens should go to /_pages/dogs/monkies_chickens.php

and a request for /dogs/ (or just /dogs) should go to
/_pages/dogs/dogs.php

Is this just a completely convoluted way of structuring a site? Essentially, I want all urls and links to look like /dogs/monkies/ etc, but I don't want to have to create so many separate directories and name all my files index.php. I also don't want to channel all requests through one index page that then includes the correct content. I have tried to below rule:

Code: Select all
Options +FollowSymlinks
RewriteEngine on
# Match up to 3 levels
RewriteRule ^([^/]*)/?([^/]*)/?([^/]*)/? _pages/$1/$2_$3.php [R]


but this gives me strange errors; I fear I simply don't understand how it all works well enough. If anyone has any suggestions as to how to achieve this, or a different way of structuring the site, please let me know, I would greatly appreciate it.[/code]
desdev
 
Posts: 3
Joined: Thu Nov 01, 2007 7:48 am

Postby richardk » Thu Nov 01, 2007 12:35 pm

Mod_rewrite isn't good at replacing a character (/) with another (_) when the amount is variable. Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule .* - [L]

RewriteRule ^([^/]+)/?$ /_pages/$1/$1.php [QSA,L]

RewriteCond %{REQUEST_URI} !^/_pages(/.*)?$ [NC]
RewriteRule ^([^/]+)/(.*[^/])/?$ /_pages/$1/$2.php [QSA,L]

RewriteRule ^(_pages/[^/]+/[^/]+)/(.+)$ /$1_$2 [NC,QSA]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Thank you!

Postby desdev » Thu Nov 01, 2007 1:34 pm

Thank you very much, that code works perfectly. I will study it now so I can try and figure out how it's working :) - Thanks again for your time!
desdev
 
Posts: 3
Joined: Thu Nov 01, 2007 7:48 am

Postby richardk » Thu Nov 01, 2007 3:45 pm

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# If it's a real file OR
RewriteCond %{SCRIPT_FILENAME} -f [OR]
# if it's a real directory
RewriteCond %{SCRIPT_FILENAME} -d
# stop rewriting.
RewriteRule .* - [L]

# Rewrite /dogs/ to /_pages/dogs/dog.php
RewriteRule ^([^/]+)/?$ /_pages/$1/$1.php [QSA,L]

# If it's not /_pages/*
RewriteCond %{REQUEST_URI} !^/_pages(/.*)?$ [NC]
# rewrite /dogs/monkies/chickens/ to /_pages/dogs/monkies/chickens.php
RewriteRule ^([^/]+/.*[^/])/?$ /_pages/$1.php [QSA,L]

# Replace slashes with underscores. This will convert one at
# a time, then run again until there are none.
RewriteRule ^(_pages/[^/]+/[^/]+)/(.+)$ /$1_$2 [NC,QSA]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

One final question

Postby desdev » Fri Nov 02, 2007 9:06 am

What about URL requests that don't actually exist. For example a request for /dogs/chickens/goats/ is fine and will server up _/pages/dogs/chickens_goats.php

but a request for /dogs/chickens/goats/owls should return a 404 not found if possible. How can I incorporate this into the rules?

Thanks again for the above code with comments, that helped immensely.
desdev
 
Posts: 3
Joined: Thu Nov 01, 2007 7:48 am

Postby richardk » Fri Nov 02, 2007 12:10 pm

but a request for /dogs/chickens/goats/owls should return a 404 not found if possible. How can I incorporate this into the rules?

It should already. It will rewrite to /_pages/dogs/chickens_goats_owls.php which will cause a 404 error.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Content

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron