Aidan Ewen February 2016

How do I stop my index.php being run twice for every user

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.

Answers


anubhava February 2016

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]

Post Status

Asked in February 2016
Viewed 2,244 times
Voted 4
Answered 1 times

Search




Leave an answer