Forum Moderators: phranque

Message Too Old, No Replies

Need subfolder to act as root

I have created a RewriteRule for this, but it's not working. What gives?

         

EivindFS

12:58 pm on Oct 25, 2007 (gmt 0)

10+ Year Member



I'm currently developing a webpage for a client of mine and need a solution to this weird challenge I'm facing. I've set up all the links on the site to refer to the root, say /img/logo.gif for the logo. This is the only practical way of doing it. ../../img/logo.gif for one file and img/logo.gif for another soon becomes unwieldy.

However, I have no root folder to install this site on. My client obviously doesn't want work in progress on their front page. So I need a subfolder to function as the root for the files inside the subfolder (and I haven't found a way to access httpd.conf on my server):

I've created two rewrite rules to fix this

RewriteRule ^/img/(.*)$ /playfilms/img/$1
RewriteRule ^/css/(.*)$ /playfilms/css/$1

and I'm linking like this: <img src="/img/logo.gif" />

This doesn't exactly work. I suspect the HTML link isn't caught by my RewriteRule as the rewrite rule is relative to the .htaccess file and that the slash doesn't point to the same place as the img src. I tried RewriteRule /img/... assuming the ^ meant "relative to .htaccess file" and that removing it would yield absolute paths, but that didn't work either.

I have managed to get the right link using this RewriteRule ^img/(.*)$ and img src="img/logo.gif", but that doesn't show even though the link is correct. It produces a server error.

Is there a way to solve this? I don't understand why this isn't working.

I hope I expressed myself clearly enough for you to figure out what's going on.

Thanks a bunch
Eivind F. Skjellum

[edited by: jdMorgan at 1:05 pm (utc) on Oct. 25, 2007]
[edit reason] No URLs, please. See TOS. [/edit]

jdMorgan

1:12 pm on Oct 25, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



URL-paths passed to RewriteRule in .htaccess do not contain the leading slash. Also, always use the [L] flag unless you need the output of one rule to be processed by a subsequent rule:

RewriteRule ^img/(.*)$ /playfilms/img/$1 [L]
RewriteRule ^css/(.*)$ /playfilms/css/$1 [L]

Flush your browser cache before testing any change to your code.

"^" is a regular-expressions token which means "must begin with." For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

EivindFS

5:20 pm on Oct 26, 2007 (gmt 0)

10+ Year Member



Hi Jim,

Thanks for posting the forum charter. Some good guidelines there. Thanks also for taking the time to explain a little.

The problem with this rewrite rule appears to be that since i'm referring to the img and css folders in the folder the .htaccess file resides in and my php files refer to the img and css folders on the root of the server, the rules won't work.

I put the same .htaccess file in the root of the server and it worked fine. The unfortunate side-effect then is obviously that my entire domain gets these redirect rules applied. I think I've seen somewhere that it's possible to specify where the user has to come from for the rule to take effect, so I'll be looking into that now.

Thanks again. Have a nice weekend
Eivind

g1smd

7:42 pm on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can use a preceding RewriteCond to test whether the following RewriteRule needs to run or not.

The RewriteCond can test if something is true, or is not true, to allow the following RewriteRule to run.

jdMorgan

9:35 pm on Oct 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you mean you are putting the rules into .htaccess files in the /img and /css folders, then you'll need to remove those path-parts from the RewriteRule pattern. All URL-paths 'seen' by RewriteRule are localized to the directory in which that RewriteRule resides. So, for example, if the 'css' rule in located in /css/.htaccess, then the rule should read:

RewriteRule (.*) /playfilms/css/$1 [L]

Jim

traypup

10:46 pm on Oct 30, 2007 (gmt 0)

10+ Year Member



I am so glad I found this thread. I am STRUGGLING with this!

I'm redesigning the whole site. I've got my redesign files in the directory /_2007redesign1/

I want everything in that directory to think it's the root so that when I copy everything TO the root, I don't have to change all of my paths.

So we did this:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^images/(.*) /_2007redesign1/images/$1 [L]

and my test page calls the images like this:

<img src="/images/bb_bg.jpg">

but if I don't have bb_bg.jpg in the ROOT image folder, it can't find the image.

Do I have something backwards or am I misunderstanding something? I want to have all of my links relative to the root in my redesign because I'm using php includes for everything that's a sitewide element (like the menu) and I want to be consistent.

Would LOVE some wise words on this. My head hurts now!
Thanks!
t.

jdMorgan

12:46 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This looks correct, so all I can come up with is four questions:

1) Did you completely flush your browser cache before testing?
2) Does a simple redirect like "RewriteRule ^foo\.html$ [google.com...] [R=301,L]" work?
3) Are there any "base href" tags in the <head> section of your pages?
4) Is the domain we're discussing the only one hosted in the 'account' or do you have add-on domains?

It's always helpful to examine (and report) the server access and error log file entries that result from testing -- The error log will often give a very good idea of what is wrong, either explicitly, or by close examination of the server filepath to which the requested/failed URL resolves.

Jim

g1smd

1:40 am on Oct 31, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I set up a password protected sub-domain named www.test.domain.com for running tests like this.

Site then fully works "as advertised", but the password keeps it from being indexed by search engines.