how to redirect one file type in root to sub directory

Jun 26, 2008 at 1:04 AM
How would I go about redirecting one file type of files to a sub directory including any possible query strings that are attached to them?  Example: -> -> ->

But this should not work: -> -> ->

Since that would cause a redirect loop to occur.

I also want to allow for this: -> -> ->

Please note that I want any file ending with .php for this to work.  Here is what I have so far which causes an endless loop and doesn't save the query:

RewriteRule /([A-Za-z0-9-]+)\.php$ /sub1/$1 [R=301,L]

I tried setting a condition rule to exclude the sub1 directory but that doesn't work:

RewriteCond %{REQUEST_URI} ^/sub1/$
RewriteRule /([A-Za-z0-9-]+)\.php$ /sub1/$1 [R=301,L]

Jun 26, 2008 at 5:31 PM

CarlaTpq, I think the endless loop is caused by the R=301 (Redirect) you are using.

When you use the redirect option, the browser sends a new request.  IIRF has an iteration limit built into it, but it applies to a single request.  because you are generating a new request, the iteration limit does not apply.  And so it continues endlessly.

Why do you redirect anyway?  It does not seem that you want or need a redirect?


Jun 26, 2008 at 6:47 PM

how about this?

# Rule for the case where there is no query string
  RewriteRule ^/(?!phpdir)([A-Za-z0-9-/]+\.php)$ /phpdir/$1 [L]

# Rule for the case where there is a query string
  RewriteRule ^/(?!phpdir)([A-Za-z0-9-/]+\.php)\?(.+)$ /phpdir/$1?$2 [L]



The ^ at the beginning of the regex says "match only at the beginning of the line."

The (?!phpdir) says, "only URLs that do not begin with phpdir."

The ([A-Za-z0-9-/]+\.php) says, "match any php URL, including any in subdirectories."

The $ says, "match at the end-of-line. This means no query string at all." This applies only in the first rule.

The \?(.+)$ (in the 2nd rule) says, "match if there is a query string."

and I guess you can figure out the replacement pattern. It's pretty simple.

Jun 30, 2008 at 9:03 PM
Well I tried it using the testDriver.exe app and it still didn't seem to work with my test queries.  So I'm just going to do a redirect instead of each file.  Not that many anyways.
Jul 1, 2008 at 12:10 AM
Edited Jul 1, 2008 at 12:10 AM

"still didn't seem to work"

I must be missing something...  for every URL you gave me, those rules "worked" for me in the testdriver.exe .