Mod Rewrite issues with analytics

We have the following code in our .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

and it works great, we have a page called about.php, you can just go to about/ and it will work. However, we’re getting a lot of weird pages showing up in Google Analytics, such as this:


Anyone have any idea what’s causing this? if you go to that page, it takes you to the IMDB page for the movie Contact.

Thanks for any help.


First, WELCOME to SitePoint’s Apache forum.

Next, add an R=301 flag (for testing purposes only) to see the redirection you’re making with your code. It will read the about/ {REQUEST_URI} and determine it’s not a directory so it’ll look for about/{DirectoryIndex} before 404’ing the request.

To use about/, you’ll have to test whether it’s a directory, remove the / if it’s not THEN redirect the new {REQUEST_URI} to its PHP’d version for your code to work properly (i.e., as you expect).

The problem with the analytics is probably caused by an Apache problem generated by the bad mod_rewrite code - this can normally be cleared (temporarily - until the bad code accumulates more problems) by a restart.



Thanks for the reply DK. I did some more research on the R-301 you mentioned, and I found this sample:

Options +FollowSymLinks

RewriteEngine On

# Remove the .php from PHP files (only files
# that exist).
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\\.php$ /$1/ [R=301,L]

# Redirect  /<name to /<name>/ if a PHP file
# or directory exists.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

# Rewrite /<name>/ to /<name>.php if
# a PHP file exists and a directory does not.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*[^/])/$ /$1.php [QSA,L]

Does that look more like what I need?

Thanks again, really appreciate it!

I’m about to walk out of the office for a while but I thought I’d respond first (and quickly).

It appears that you want to CREATE A LOOP with your mod_rewrite, i.e., remove the .php extension, redirect, then add it back in to serve the script!

My reply to a member ages ago was, no, it can’t be done. On second thought, I created a script for her to use which adds a marker to the query string which can prevent the loop but remain out of sight. READ the tutorial linked in my signature and look for the code there!