Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirecting non-existing directories

         

blulegend

9:12 am on May 1, 2007 (gmt 0)

10+ Year Member



I'm trying to redirect non-existing directories to another domain.

If
mydomain.com/nonexistantdirectory/*
Redirect to
otherdomain.com/nonexistantdirectory/*

Or I can have it redirect all directories except the ones I specify.

If
Not mydomain.com/existantdirectory OR
Not mydomain.com/anotherexistingdirectory
Redirect to
otherdomain.com/directoryrequested

I also want to redirect the root directory to a specific directory on another domain.

If
mydomain.com
Redirect to
otherdomain.com/specificdirectory

Can anyone help? I've been trying all night using mod_rewrite and I can't get it. Thanks so much.

[edited by: jdMorgan at 3:31 am (utc) on May 4, 2007]
[edit reason] De-linked. [/edit]

jd01

12:31 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi bluelegend,

First:
Welcome to WebmasterWorld!

Second:
Could you please post an exemplified version of what you have been trying to use, with some more specifics? (Then we can help you solve the problem, rather than writing the code for you. :))

I'm not sure I understand what you are trying to do…
Redirect the whole domain to different sites?
Redirect only some pieces?
Are there some directories you will still need to have access to?

You might also want to have a look in the Apache Library [webmasterworld.com] for tutorials and explanations.

Justin

g1smd

11:09 pm on May 1, 2007 (gmt 0)

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



I have to ask, why are you doing this?

I can't imagine the purpose. Fill in some details please.

blulegend

1:12 am on May 2, 2007 (gmt 0)

10+ Year Member




Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*)$ http://www.flickr.com/$1 [R=301,L]

I'm trying to redirect any files/directories not on my server to my flickr account. When I try the above it redirects everything, even if it exists on my server.

[edited by: jdMorgan at 3:32 am (utc) on May 4, 2007]
[edit reason] de-linked. [/edit]

blulegend

1:18 am on May 2, 2007 (gmt 0)

10+ Year Member




Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.flickr.com/$1 [R=301,L]

Looks like this code works! Let me know if there's anything wrong with it that I'm not aware of.

[edited by: jdMorgan at 3:31 am (utc) on May 4, 2007]
[edit reason] de-linked. [/edit]

jdMorgan

1:35 am on May 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That's because the original code you used says, "If the requested resource exists as a file or exists as a directory, then redirect." So, to correct the sense of the logic and use the (default/implicit) AND operator instead of the explicit local [OR], you could use:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.flickr.com/$1 [R=301,L]

Bear in mind that this technique will likely results in some weird search engine treatment of your site, since some search engines occasionally test the 404 response of your serve by requesting a non-existent file...

Jim

[edited by: jdMorgan at 2:37 am (utc) on May 2, 2007]

blulegend

8:20 am on May 2, 2007 (gmt 0)

10+ Year Member



jdMorgan, awesome. I didn't even think of streamlining my logic there. Took out that line and reversed the logic. Thanks.

The reason I'm doing this is because I've migrated my photo gallery to flickr. I want to send users to flickr who are viewing my base directory or are following links I can now place that are parallel to flickr's directory structure.

However, I have some directories that I've used to host my own images that are embedded in websites. For those files that exist on my server, I want to still serve them as usual.

jdMorgan

12:52 pm on May 2, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Right, just make sure your robots.txt, /w3c/p3p.xml, and labels.rdf files never go missing. If they do, then conceivably, anyone could post a replacement to flickr and hurt your site. This would be true for any script or CSS files as well -- Any file that's missing from your server can be replaced by a file on flickr, with attendent effect on search engine rankings, content filters, or site appearance... And because you're redirecting missing files immediately, you won't see a "404" as a warning in your log files or stats.

In this case, look for anything about the filenames that you can use to make the redirect more exclusive; For example, redirect only missing media \.(jpe?g¦gif¦png¦wmv¦mpe?g¦mp3)$ files, or redirect only files in certain directories -- or both.

Jim

blulegend

1:09 am on May 4, 2007 (gmt 0)

10+ Year Member



How would I change it to redirect only certain directories?

g1smd

1:15 am on May 4, 2007 (gmt 0)

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



You could list them in the RewriteCond part, with [OR] statements.