1

We've got a webapp where the initial html is served up using apache and php.

I recently noticed that index.php was being run twice for every request. This seems to be caused / effected by the rewrite rules in our .htaccess -

RewriteCond %{HTTP_HOST} ^(www\.|hotels\.)ourdomain\.com$ [NC,OR]
RewriteCond %{SERVER_NAME} ^(www\.|hotels\.)ourdomain\.com$ [NC]
RewriteRule (.*) http://ourdomain.com/$1 [R=301]

RewriteRule ^hotels/([^/]+)/?\??(.*)$ ?d=$1&$2 [QSA]

The final rule is moving a parameter from the url path to the query string. i.e.

http://ourdomain.com/hotels/vegas?someParam=1

becomes

http://ourdomain.com?d=vegas&someParam=1

If I go directly to the query string version then index.php is only run once. However if use a url that will be redirected, then index.php is run twice (I'm checking by adding error_log('end of index.php') to the file).

So for example going to http://ourdomain.com/hotels/paris hits the file twice where as http://ourdomain.com?d=paris only hits it once.

I've seen this question and had a look at the blog mentioned, and I can't find any empty string url's (I've tried using yslow for this process).

Can anybody tell me why this is happening? or how I fix it?

EDIT

It looks like it's a javascript error I'm getting http requests like "GET /hotels/undefined HTTP/1.1" in the access log.

Community
  • 1
  • 1
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88

1 Answers1

1

Try last rule as:

RewriteRule ^hotels/([^/]+)/?$ index.php?d=$1 [NC,QSA,L]

Or catch the erroneous requests with

RewriteRule ^hotels/((?!undefined)[^/]+)/?$ index.php?d=$1 [NC,QSA,L]
Aidan Ewen
  • 13,049
  • 8
  • 63
  • 88
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks - I'm afraid index.php now runs 5 times instead of just 2. – Aidan Ewen Feb 08 '16 at 18:57
  • :) then I think your test is not correct. Enable rewrite log and then check how many times `index.php` is invoked – anubhava Feb 08 '16 at 18:59
  • Sorry, you're right. still twice. (for some reason our session logger is logging multiple sessions now - which is actually the original bug I was trying fix). Thanks for you time anyway. – Aidan Ewen Feb 08 '16 at 19:06
  • Yes `GET /hotels/undefined HTTP/1.1` will cause your `index.php` to be invoked again. You can avoid it by using: `RewriteRule ^hotels/((?!undefined)[^/]+)/?$ index.php?d=$1 [NC,QSA,L]` – anubhava Feb 08 '16 at 19:08
  • 1
    Thanks @anubhava - that makes sense. It's pretty hacky though. I guess if I can't find the js responsible, I might have to try it. – Aidan Ewen Feb 08 '16 at 19:13
  • 1
    Wow - not easy! think something's writing undefined to a img src attribute (accept image/png,image/*;q=0.8,*/*;q=0.5 in the http request), but what's doing it is very difficult to identify. Actually went with RewriteRule ^hotels/undefined - [L,R=404] in the end. – Aidan Ewen Feb 08 '16 at 20:50