Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite subfolders?

         

gehrlekrona

12:11 am on Sep 26, 2007 (gmt 0)

10+ Year Member



In my .htaccess file I have this:

RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule .* - [L]

RewriteRule ^search/([^/]*)/([^/]*)\.html$ /src/index.php?cid=$1&br=$2 [L]

What is does is to rewrite a file to a virtual folder like this:
www.example.com/src/2/a.html which is fine and dandy.
Now to my problem.
I have a "real" subfolder that I don't want to be re-written so that why I added the request-filename and that works fine to BUT if I have a file in the "real" folder with querystring i chokes and I don't know how to get it right.
this works:
http://www.example.com/srch/utah/
this does not work:
http://www.example.com/srch/utah/index.php?cat=1&dat=2
I need the last one to stay the same, not to be re-written...
Is it even possible?

[edited by: jdMorgan at 1:24 am (utc) on Sep. 26, 2007]
[edit reason] example.com [/edit]

jdMorgan

1:22 am on Sep 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm not really sure why you're having a problem; As described, your code should work.

But partly as a test, and partly as a major efficiency improvement, have you tried a simpler approach like:


RewriteCond %{REQUEST_URI} !^/src/index\.php$
RewriteCond %{REQUEST_URI} !^/srch/
RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteRule ^search/([^/]*)/([^/]*)\.html$ /src/index.php?cid=$1&br=$2 [L]

That is, do the rewrite unless the request is for /robots.txt, /src/index.php, or any URL-path beginning with "/srch/". The exclusion for "/src/index.php" prevents an 'infinite' rewrite loop.

This approach has the advantage that it does not force two extra searches of your filesystem for each and every HTTP request -- checks that can quickly bog down a busy server... It also has the advantage of being quite explicit about what is and is not to be rewritten.

Jim

[edited by: jdMorgan at 1:23 am (utc) on Sep. 26, 2007]

gehrlekrona

2:00 am on Sep 26, 2007 (gmt 0)

10+ Year Member



Seems to be working for the most part.

I have this file that I don't want to be rewritten but it still tries to rewrite it:

domain.com/src/new_york/index.php?searchterm=NY&category=y

What I would like to is to keep it as it is and not do any rewrite.
I appreciate you trying to help :)

jdMorgan

2:24 am on Sep 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about just not rewriting any "index.php" file in any directory? That makes the code even simpler:

RewriteCond %{REQUEST_URI} !/index\.php$
RewriteCond %{REQUEST_URI} !^/robots\.txt$
RewriteRule ^search/([^/]*)/([^/]*)\.html$ /src/index.php?cid=$1&br=$2 [L]

The point here is that a simple solution is best -- more efficient, easier to understand and maintain... You can of course add a bunch of case-by-case exclusions, but a "rules-based" method is often more efficient.

Jim

[edited by: jdMorgan at 2:24 am (utc) on Sep. 26, 2007]

gehrlekrona

2:43 am on Sep 26, 2007 (gmt 0)

10+ Year Member



This is what I get if I do that:

Seems like it doesn't care about the index.php part....

[domain.com...]

jdMorgan

1:22 pm on Sep 26, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That is not the exact type of URL you should be requesting in order to invoke the rewrite. That URL has three parts, src, y, and ny.html, while your rule is set up to accept only two parts, and starting with "search", not "src".

Please remember that we cannot look over your shoulder, so we do not know what you expect to happen in this case or in other cases. When you post about a test, please let us know what URL-path you requested, what you expected to happen, what the actual results were, and how the actual results differed from your expectations. Doing this every time makes collaborative debugging much more efficient and faster.

Jim

gehrlekrona

1:40 pm on Sep 26, 2007 (gmt 0)

10+ Year Member



yeah I know...
It's hard when you can't have "real" examples....
the src IS search.....

If I say that this is the only thing I want rewritten and nothing else:

RewriteRule ^search/([^/]*)/([^/]*)\.html$ /src/index.php?cid=$1&br=$2 [L]

What can you do to stop it from rewriting anything I have in subfolders to domain.com/search/?