Add two directories using Mod_Rewrite?

New to mod_rewrite? This is a good place to start.

Add two directories using Mod_Rewrite?

Postby i-CONICA » Sat Jul 11, 2009 9:22 am

Hi, Richard. :D

I need to be able to put a huge list of subdirectories into a subfolder so that they're all contained in one place on the server. Away from all the other sites files. But the URLs cannot have this naming convention.

For instance:
domain.com/animals/horses/white-ones/
Where animals is just one of many many categories.

So to keep all these categories from cluttering up the root of the domain, i'd want to put them into a folder called categories. But I cannot have that in the URLs.
So I want to put the category animals into a subdirectory called categories but make categories invisible to the users.

How can I do this with Mod_rewrite?

Thank you! :o
i-CONICA
 
Posts: 13
Joined: Tue Aug 26, 2008 9:24 pm

Postby richardk » Sun Jul 12, 2009 3:01 pm

/categories/animals will be a directory?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/categories/$1 -d
RewriteRule ^([^/]+)(/.*)?$ /categories/$0 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby i-CONICA » Mon Jul 13, 2009 1:03 am

Hi, Richard.

Thanks for the reply.

The directory on the server is simply to keep all the categories etc.. in one neat place.

The idea is, I can have things layed out like this on the server.

/anydirname/animals/horses/brown-horses/

But /anydirname/ is invisible from the web. So I can link to
/animals/horses/brown-horses/ and it will resolve. Accessing
/anydirname/animals/horses/brown-horses/
on the server.

I hope that clarifies what I'm trying to do.

Thank you.
i-CONICA
 
Posts: 13
Joined: Tue Aug 26, 2008 9:24 pm

Postby richardk » Wed Jul 15, 2009 12:40 pm

You have made it less clear. Does anydirname change?

Have you tried the mod_rewrite i provided?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby i-CONICA » Wed Jul 15, 2009 5:47 pm

Hi, Richard.

No, the directory name will never change.

It's simply so I can keep all the hundreds of categories in one neat place on the server but that place will be invisible to the users.

On the server:
/neatplace/animal/horse/

linked on the site:
/animal/horse/


I hope that clarifies it. :)

ps. I have tried the code, it didn't work.. :( I changed categories with the actual directory name obviously...
i-CONICA
 
Posts: 13
Joined: Tue Aug 26, 2008 9:24 pm

Postby richardk » Thu Jul 16, 2009 1:48 pm

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{REQUEST_URI} ^(/[^/]+)(/.*)?$
RewriteCond %{DOCUMENT_ROOT}/categories%1/ -d
RewriteRule . /categories%{REQUEST_URI} [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby i-CONICA » Thu Jul 16, 2009 1:52 pm

Spot on! Works perfectly.

Thank you for the patience, and thank you for the code too. :)
i-CONICA
 
Posts: 13
Joined: Tue Aug 26, 2008 9:24 pm


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 105 guests

cron