Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

htaccess yields new wrinkle

6:34 pm on Apr 2, 2013 (gmt 0)

Was having trouble when a client registered a new domain name and because the server was not set up in parallel, it had to point to a sub directory inside of the main domain structure. Because of that, the images had to be routed through the main domain. While I could just add the path hard-coded or echoed out in php, I was looking for a more elegant solution.

Was finally able to get the images from the sub directory to show up using an htaccess:

RewriteRule ^(.*)http://newdomain.com$(.*) http://accountdomain.com/newsitefolder/$2 [L,R=301]

Everything was great until I looked at the gallery page. Those images are NOT showing.

The first site is broken up into three parts, for three territories. Each with most of the same stuff in each directory, but much of the text is different.
I had a directory that held the images for the gallery outside of the three territories so they could share them.
I referenced their path with ../gallerypics/image1.jpg in both js and the photo page itself.

Now with the rewrite rule, I'm lost as to why they are not showing up nor how to fix it.

I'm very inexperienced with htaccess, so please explain everything as you go.

[edited by: bill at 4:12 am (utc) on Apr 3, 2013]
[edit reason] unlinked example [/edit]

11:31 pm on Apr 2, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

RewriteRule ^(.*)http://newdomain.com$(.*)

What the bleep bleep? What on earth do your URLs look like? In htaccess, with this rule you're looking at something in the form

http://www.example.com/{optional-stuff-here}http://newdomain.com{syntax error here}

Frankly I'm surprised your system didn't simply shut down when faced with an ending anchor $ followed by the possibility of more text.

Was finally able to get the images from the sub directory to show up

Where did they show up? In some kind of local testing with a WAMP-type pseudo-server?

Anyway, wasn't there an almost identical question just a week or so back? An add-on domain physically located in a subdirectory within the primary domain, but using images that lived at the primary domain. I have already forgotten the answer, except that they seem to have gotten it to work without using any proxy business.
3:35 pm on Apr 3, 2013 (gmt 0)

So you're saying it should look more like this?

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^newdomain.com.*$ [olddomain.com...] [R=301,L]

Remember I'm very new at this...
9:09 pm on Apr 3, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

Yes. The pattern should give only the path, not the domain. In htaccess, there is no leading slash. If the pattern has an opening anchor ^ it refers to the directory the htaccess file is located in.

Unless the rule itself is inside a <Files(Match)> envelope. But let's not go there.

Relative links are perilous-- especially ones in the form ../ Some people hereabout will tell you never ever to use them. They're useful if you have areas that always function as a package: for example a directory that contains its own, directory-specific /images/ subdirectory. And, of course, they're great for offline testing because the browser itself knows how to handle relative links of all kinds, so you don't have to fire up the pseudo-server.

But if you've got stuff happening server-side, there is probably no longer a reason for ../ links.
3:18 pm on Apr 5, 2013 (gmt 0)

One more question. I have an responsive image script that uses htaccess and js.

<IfModule mod_rewrite.c>
RewriteCond %{REQUEST_URI} !assets

Since we are rewriting, what is the best way to make this addition?
9:42 pm on Apr 5, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

The only thing I can say for sure is: Get rid of the "IfModule" envelope. Beyond that, you need to integrate the contents of the envelope with the rest of your mod_rewrite activity. In general this means two layers of nesting: from most severe [F] to least severe [L] passing through [G] and [R=301] along the way. And within each of these, go from most specific to most general.

I stress: in general. In my current htaccess for example there are two exceptions. I've a redirect tucked among the [F] rules --because it's a particular type of redirect that's functionally similar to a 403-- and another tucked into the [G] section --because the affected file would otherwise be reported as 410.
3:10 pm on Apr 8, 2013 (gmt 0)

Thank you! Can you give me a short lesson in what these letters mean? I'm lost...
9:09 pm on Apr 8, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

If you don't recognize them it means you don't currently use them-- which can only be because you don't need them. (There are gray areas in mod_rewrite, but this is not one of them.) They are mod_rewrite flags:

[F] = Fail = 403. Exactly the same as "Deny from..." in mod_authz-whatever. Will come through as 403 in your logs.
[G] = Gone = 410. Use this when you have deleted files; it makes the googlebot stop crawling sooner. (Has no visible effect on bing.) Make sure you have a nice custom 410 page in place if you ever need to use this flag.
[L] = Last = stop rewriting here. Normally you include this flag in ALL RewriteRules, except when there's another flag that implies [L]. That includes F and G-- but not R, which is why you generally see them paired as [R=301,L]. RewriteRules without [L] are for grownups. I don't touch them myself.*

When you have a bunch of different RewriteRules doing different things, you need to put them in the right order. Start with the absolute lockouts: No point in redirecting someone if you're going to end up slamming the door in their face. Then the no-longer-here pages: If you're not redirecting to some specific other page, there's no reason for the visitor to stick around any longer. Once this is out of the way, you get to the redirects-- the ones where the browser's address bar changes.

After everything else are the pure rewrites-- the ones ending in [L] alone.

Where the htaccess parts of your image script belongs will depend on what exactly happens in the script. Since it uses an <IfModule mod_rewrite... envelope you have to assume there is rewriting involved ;) But if the script involves only image files (extensions like .png or .jpg) and all your other RewriteRules involve only requests for pages, it may not really matter what comes first.

* Er, except for the two limited-use cookies I set in mod_rewrite. After I figured out that they're not mutually exclusive, I had to remove a couple of [L] flags. Oops.

Featured Threads

Hot Threads This Week

Hot Threads This Month