Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

htaccess has always given my problems. Every time I think I understand it, htaccess goes against what I just thought I understood. Any help would be GREATLY appreciated.

What I want to do with htaccess is redirect URI's like:

www.example.com/get/something to  www.example.com/get.php/something
www.example.com/cape/something  to www.example.com/active.php/something

Along with a bunch of others. I have that working great, the thing I don't have working is if none of those rules are satisfied then to redirect to my main domain. So if I end up with a URI of www.example.com/anythingThatIsNotDefined/anything or just www.example.com/anythingThatIsNotDefined I want to redirect that to http://www.anotherexample.com

It is that last part I can not get to work. My current htaccess file is:

RewriteEngine On
RewriteRule ^get/(.*)$ /get.php/$1 [L]
RewriteRule ^skin/(.*)$ /active.php/$1 [L]
RewriteRule ^cape/(.*)$ /active.php/$1 [L]

What do I need to modify to get anything that is not get or skin or cape to redirect to my other domain?

I will need to add more than get, skin, and cape in but once I get it working I should be able to add more in easily

Thanks in advance, this has really been stumping me.

(If this is not the right stack site I apologize, it was a 50/50 toss up between here and overflow. I was not sure which one it belonged on.)

share|improve this question

1 Answer 1

up vote 3 down vote accepted

.htaccess uses order to determine what happens first (as do all Apache config files). In your case, you simply need to place all of your "special" rewrite rules before a catch-all rule like the one below.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.otherdomain.com/$1 [R=301,L]

This rule basically says that for anything which doesn't exist as a hard resource (file or directory), redirect the request to http://www.otherdomain.com, including the URI string. Assuming this comes after your friendly-URL redirects, they won't be caught.

share|improve this answer
    
Thank you, that worked. If I wanted to not allow any resources to be accessed (besides the ones I specify) could I just get rid of the conditions? Edit: I tested it without the conditions and that works perfectly for what I needed. Is there any reason either condition should be kept if I want to disallow anything that was not specified? Or was removing both the correct thing to do there? –  Ryan Sullivan Mar 22 '12 at 21:25
    
Yes and no. Read this page for more info on this concept: httpd.apache.org/docs/current/rewrite/flags.html#flag_l –  Garrett Mar 22 '12 at 22:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.