Forum Moderators: phranque

Message Too Old, No Replies

using RewriteRule to point some root directories to sub directories

         

fintan

10:12 am on Jun 9, 2008 (gmt 0)

10+ Year Member



Hi

I'm trying redirect links looking for images and css in the root to sub directories depending what sub directory the site is in. This /en/media/ stays the same it's the sub directories that change.

RewriteEngine on
RewriteRule /en/media/ /varible-sub-dir/en/media/ [R]

Any help please? Thanks

fintan.

jdMorgan

9:56 pm on Jun 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure I understand why you'd want to do a redirect, but the modification to your rule above would probably involve adding a RewriteCond to examine %{HTTP_HOST} and extract the subdomain name.

If you do want to use a redirect, I suggest you use a 301 redirect, not a 302, or you won't be telling search engines to stop using the old URL.

I suspect you'll be happier if you take the requested subdomain and URL-path, and internally rewrite to a different filepath. In this way, the URL stays the same; Only the filepath used to deliver content for that URL changes.

Also, always use an [L] flag on your rules, unless you have a specific reason not to.

Jim

fintan

7:49 am on Jun 10, 2008 (gmt 0)

10+ Year Member



Thanks jdMorgan for the reply. I inherited the system from someone else.

I'll try to explain it better.

I've 10 directories hanging off root with the same structure. Say each has a sub directory with media/images. So it would be /a/media/images, /b/media/images and so on. How would I rewrite all links that point to /media/images to point to
/a/media/images, /b/media/images, /c/media/images, etc...
depending on which directory they are in a, b, c, etc..

It's internal so I'm not that concerned about search engines.

What I've got so far is.

RewriteEngine on
# Only apply to URLs on this domain
RewriteCond %{HTTP_HOST} ^mydomain$
# Only apply to URLs that are under /media/images/
RewriteCond %{REQUEST_URI} ^/media/images/

This is where I fall down.

If it was something like this it'll be easy

RewriteRule /([a-z])/media/images /media/images/$1

but I need the variable on the other link like this
RewriteRule /en/media/ /a¦b¦c¦etc/en/media/

Is there a way to carry a variable from a RewriteCond and pass it into a RewriteRule?

jdMorgan

10:51 pm on Jun 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Is there a way to carry a variable from a RewriteCond and pass it into a RewriteRule?

Yes, it is a basic function of mod_rewrite. And rather than waiting around all day for an answer in a forum, it would likely be faster (and a better investment) to read the Apache mod_rewrite documentation [httpd.apache.org] -- specifically the notes in the RewriteCond and RewriteRule sections on "back-references."

Jim

fintan

12:40 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Ok I've gone through the manual and this is what I've got.

# Find which directory we want
RewriteCond %{REQUEST_URI} ^/(a¦b¦c¦d)/.*
RewriteCond %{REQUEST_URI} ^/media/.*
RewriteRule ^/.*/ /%1/
RewriteRule ^/(media)/(.*) /%1/$1/$2

How would I carry over a¦b¦c¦d into the last rewriterule?

jdMorgan

12:53 pm on Jun 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first rule won't work, because it requires the subdirectory to be both "/a¦b¦c¦d" *and* "/media" at the same time. However, a more-serious problem is that of design: How is the server to determine which of a¦b¦c¦d it is supposed to rewrite to? In your first post, you said, "depending what sub directory the site is in." So how is mod_rewrite to determine "what sub directory the site is in" based on the requested URL? Or is it your intent to have mod_rewrite "search" through all those directories to find the directory in which the image resides?

Jim

[edited by: jdMorgan at 12:53 pm (utc) on June 12, 2008]

fintan

1:42 pm on Jun 12, 2008 (gmt 0)

10+ Year Member



Yeah I was figuring that. The way I see it I could rewrite the html on the fly or check if to see if every file exists first in each directory.

Neither is a good solution. Would there be another way to do this beside rewriterule?