Forum Moderators: phranque

Message Too Old, No Replies

.htacces redirect from /first-sub-directory/ only

there's got to be a trick to doing it right?

         

chewy

12:18 am on May 5, 2012 (gmt 0)

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



You guys are good. You've saved my butt many-a-time.

Can you help me figure out a simple way to do this?

So I've discovered that somebody somewhere backlinked to mydomain.com/first-sub-directory/

...which is a bad link (eg, there is no index.htm in that folder. So it returns a 404.

of course there are items in that folder like this:

/first-sub-directory/page1.html
/first-sub-directory/page2.html

etc.

So I've discovered the hard way that If I redirect the /first-sub-directory/ back to the root of the site, it redirects all the traffic to the inner pages on that path, not just the traffic to the / of the first sub directory.

Is there a simple way to redirect traffic coming from the off-site bad link that is supposed to go to the /first-sub-directory/ to the root without redirecting ALL the traffic to the root?

-C

g1smd

12:51 am on May 5, 2012 (gmt 0)

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



Use a RewriteRule and use a RegEx pattern that matches exactly the folder with trailing slash and no more.

phranque

2:48 am on May 5, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



you probably need to add a $ end-anchor meta character to your regex.
post your current mod_rewrite code (exemplified) if you can't figure it out.

lucy24

3:04 am on May 5, 2012 (gmt 0)

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



Alternately, you can take your best guess which page they had in mind, and redirect that way. I've a whole slew of directories that for various reasons contain user-visible pages but no index files, so I've dealt with this one before. For example:

RewriteRule hovercraft/nunangat(/(index\.html)?)?$ /hovercraft/nunangat/UraniumSplash.html [R=301,L]
or
RewriteRule paintings/maggie(/(index\.html)?)?$ /paintings/maggie/maggie.html [R=301,L]

Note the closing anchor. That's how you keep from redirecting all the other requests, the ones for files that do exist. The parentheses and question marks are to cover all the ways that a human might possibly type the name of the nonexistent index file.

chewy

3:31 am on May 5, 2012 (gmt 0)

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



is it as simple as using a "$" as such?

RewriteRule first-sub-directory/$ /first-sub-directory/page1.html

?

g1smd

6:08 am on May 5, 2012 (gmt 0)

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



Yes. The $ is the key.

And as per previous examples you should also cover instances where the user types index.html or index.php on the end.

However, you have missed several important things.

Your code is now for an internal rewrite. When user requests
example.com/first-sub-directory/
the user will see the content of
/first-sub-directory/page1.html
at the original URL they requested. If you want a redirect the target should include the protocol and domain name. That gets you a 302 redirect. To make it a 301 redirect add the [R=301,L] flags.

Additionally, you haven't included a start anchor. This means that your rule will also be invoked when
example.com/<random-anything>/first-sub-directory/
or
example.com/<random-anything>first-sub-directory/
or or
example.com/<random-anything>/<random-anything>first-sub-directory/
is requested. For an internal rewrite, this is a very bad idea as it allows infinite duplicate content to be served.

incrediBILL

8:14 am on May 5, 2012 (gmt 0)

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



You guys are good. You've saved my butt many-a-time.


Thank you!

Now it's time to click DONATE [webmasterworld.com] to keep this place running.

Heck, I'm a moderator, I give my time freely and I'm a paid subscriber.

Call me insane, but you get what you pay for ;)

chewy

2:13 pm on May 5, 2012 (gmt 0)

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



incrediBill - I'm a happy official "supporter".

The WebmasterWorld robot hits my PayPal account every year, (since before I can remember) whether Mr Robot asks me or not - and I love to support WebmasterWorld!

chewy

2:16 pm on May 5, 2012 (gmt 0)

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



Glenn, where's that "like" button when you need it?

So long from Cape Cod,

Thanks everybody!