Forum Moderators: phranque

Message Too Old, No Replies

Rewriting to bootstrap problems

Adding query string to URL on no trailing slash

         

ogryn

11:22 am on Nov 29, 2007 (gmt 0)

10+ Year Member



Hi,

I have a htaccess rule like:


RewriteRule ^(directory_1/.*)$ /bootstrap.php?url=$1/ [QSA,L]

I want any file in directory_1 (or it's subdirectories) to be redirected to bootstrap, with the requested URL passed in as a GET variable.

It currently works fine for well formed REQUEST_URI's, but if I miss a trailing slash off a directory, then the URL gets changed to

directory_1/directory_2/?url=directory_1/directory_2/

The page still loads, but I don't want that?URL to be added to the address.

Any ideas as to how to stop this from happening? I've spent a couple of days tinkering with this now and am getting quite frustrated.

Many thanks in advance.

jdMorgan

1:27 pm on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is pretty confusing. It sounds like mod_dir is interfering with your mod_rewrite. Or perhaps you are getting recursion. But neither are obviously likely, given your description of the URL-paths involved. So a few questions:

Is this a commercially-hosted server, or did you configure it yourself?
Are the example URL-paths above completely accurate?
Does directory_1 ever contain the string "bootstrap.php"?
Where is bootstrap.php located in relation to directory_1?

Jim

ogryn

2:48 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



Is this a commercially-hosted server, or did you configure it yourself?

It is our own server. Running CentOS, and Apache 2.0.52

Are the example URL-paths above completely accurate?

Yup.

Does directory_1 ever contain the string "bootstrap.php"?
Where is bootstrap.php located in relation to directory_1?

Bootstrap.php is in the DocumentRoot. It never exists anywhere else in the code.

mod_dir appears to be working, as it adds the trailing slash before the?url=directory_1/directory_2, but I just have no clue as to why the url gets added... could it be a bug in the Apache version?

jdMorgan

3:20 pm on Nov 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I doubt it's a bug -- There are after all, thousands of deployed servers.

I think we're looking at a recursion problem here, where a redirect (from mod_dir, possibly) is 'exposing' your query string parameters. But since the ^directory_1 pattern does not match "bootstrap.php" URL-path, the scope of this problem must include other rules, directives, or possibly scripts beyond the code that you've posted.

In particular, if you have code intended to redirect browsers and search engine 'bots which request the dynamic URLs, then that code is likely involved in this problem.

One possible solution is to add mod_rewrite code to fix-up the missing slash "manually" without letting mod_dir do it. That has the advantage that you can explicitly control order of execution, because all directives within a given Apache module are executed in the specified order (this is not true if you mix directives from different modules; Those are executed on a per-module-ordered basis).

So, trying to get the complete requirements nailed down before we start coding, do you have any other 'agents' which may be redirecting, rewriting, aliasing, or 'meta-refreshing' URLs of the 'directory_1' or 'bootstrap.php' format?

Jim

ogryn

5:50 pm on Nov 29, 2007 (gmt 0)

10+ Year Member



As far as I can tell, no. I have no other .htaccess files and I can't find anything similar in /etc/httpd/conf/httpd.conf

Whilst trying some debugging this afternoon, I noticed a similar thread here relating where the solution was to use:

RewriteCond %{REQUEST_FILENAME}!-d
[Rewrite Code]

RewriteCond %{REQUEST_FILENAME} -d
[Rewrite Code]

[Final Rewrite Code]

Funny thing was that the -d never seemed to get triggered, even if we were requesting a directory.

I may have to give up on this and apply a workaround methinks. Thanks for your time.