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:

http://www.domain.com/test1.php -> http://www.domain.com/sub1/test1.php
http://www.domain.com/test1.php?test=1 -> http://www.domain.com/sub1/test1.php?test=1
http://www.domain.com/test1.php?test=1&test2=2 -> http://www.domain.com/sub1/test1.php?test=1&test2=2

But this should not work:

http://www.domain.com/sub1/test1.php -> http://www.domain.com/sub1/test1.php
http://www.domain.com/sub1/test1.php?test=1 -> http://www.domain.com/sub1/test1.php?test=1
http://www.domain.com/sub1/test1.php?test=1&test2=2 -> http://www.domain.com/sub1/test1.php?test=1&test2=2

Since that would cause a redirect loop to occur.

I also want to allow for this:

http://www.domain.com/test/test1.php -> http://www.domain.com/sub1/test/test1.php
http://www.domain.com/test/test1.php?test=1 -> http://www.domain.com/sub1/test/test1.php?test=1
http://www.domain.com/test/test1.php?test=1&test2=2 -> http://www.domain.com/sub1/test/test1.php?test=1&test2=2

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]

Coordinator
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?

 

Coordinator
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]

?? 

Explanation:

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.
Coordinator
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 .