views:

948

answers:

2

I am currently using the following rules in an htaccess file:

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

This works well to ensure that myfile.php works as well as just myfile (with no extension). It also handles querystring's with no problems, so myfile?var=foo also works.

The problem is that these are registering in Google Analytics as being too seperate files. So while myfile.php might be the third most popular page on my site, with X visits, myfile might be the fifth most popular page on my site with Y visits.

Is there a way for me to do a hard redirect, rather than "accept either one" type of rule?

Thanks

+1  A: 

Try this rule to redirect requests for .php files:

RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php
RewriteRule ^.+\.php$ /%1 [L,R=301]
Gumbo
I added this in between the first and second lines. It appears to have not made any effect.
Cory Dee
So if you request “/foo/bar.php” you are not being redirected to “/foo/bar”?
Gumbo
No, it's still loading /foo/bar.phpI also had to move the closing bracket, as it would cut out a directory when used in certain folders.
Cory Dee
+2  A: 

Could you just change your last RewriteRule to do this?

RewriteRule ^(.*)$ $1.php [L,R=301]

That will redirect "myfile" to "myfile.php".

Also, you can adjust for this inside of Google Analytics using rules as well. Although that probably isn't an ideal solution.

ETA: If you want myfile.php to redirect to myfile, try this for your last rule instead:

RewriteRule ^(.+)\.php$ $1 [L,R=301]
Eric Petroelje
This makes the .php page stay as .php. The page with no extension goes from:http://localhost/2008/about/managementtohttp://localhost/C:/Program%20Files/Apache%20Group/Apache2/htdocs/2008/about/management.php
Cory Dee
Isn't that what you were looking for? Or did you want "myfile.php" to redirect to just "myfile"?
Eric Petroelje
Yes, myfile.php to myfile...but it shouldn't be adding in C:/Program Files etc etc into my URL. That creates an invalid path.
Cory Dee
Yikes, not sure why it would it be adding C:/Program Files to your URL.. Maybe you need a "RewriteBase /" in there?
Eric Petroelje