301 redirect with wildcard - need help

4 replies
I'm having some problems with a redirect.

I have A LOT of 404s regarding 8 specific PHP files in many different directories.

What I would like to do is make a redirect for any file called e.g. test.php in any folder and redirect it to a file by the same name (I dont know if that is actually possible with wildcards) in the domain root.

Like...

/folder1/test.php
/folder2/test.php
/folder2/sub1/test.php
/folder3/test.php
/folder3/sub1/hello/test.php

Redirected to: http://domain.com/test.php

I've tried many options and Googled a lot, but I havent succeeded yet other than making a neverending 301 loop, which isn't exactly good.

I hope someone is able to help me here.

Thanks.

Martin
#301 #redirect #wildcard
  • Profile picture of the author Iconiplex
    In your root .htaccess file:
    Code:
    RewriteEngine On
    RewriteRule ^(.*)/test.php /test.php [R=301,L]
    Signature
    US-based design, development, marketing, branding, printing, and all other business services.
    High-risk payment processing for pharma, e-cigs, seeds, and more!
    Accepting investment management clients. Minimum deposit is $3000.
    Skype/AIM: artizhay
    {{ DiscussionBoard.errors[8944097].message }}
    • Profile picture of the author David Beroff
      I suspect that that solution will yield the exact infinite loop to which the OP refers. I think there needs to be an end case so that the redirect stops, e.g., don't match if there are no directories in the path.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[8944250].message }}
      • Profile picture of the author Iconiplex
        Originally Posted by David Beroff View Post

        I suspect that that solution will yield the exact infinite loop to which the OP refers. I think there needs to be an end case so that the redirect stops, e.g., don't match if there are no directories in the path.
        Did you actually test the code like I did, or are you proposing unfounded speculation that will ultimately prove to be false, thus further confusing the OP? Any path leading to test.php in a subfolder like /sub1/test.php will redirect to /test.php, but /test.php will never redirect to /test.php.
        Signature
        US-based design, development, marketing, branding, printing, and all other business services.
        High-risk payment processing for pharma, e-cigs, seeds, and more!
        Accepting investment management clients. Minimum deposit is $3000.
        Skype/AIM: artizhay
        {{ DiscussionBoard.errors[8944282].message }}
        • Profile picture of the author David Beroff
          Fair enough. You're right; I didn't initially see that the ".*" won't match the top-level URL, so there's the end condition. My apologies!
          Signature
          Put MY voice on YOUR video: AwesomeAmericanAudio.com
          {{ DiscussionBoard.errors[8944289].message }}

Trending Topics