Retain .php extensions in Drupal 6

Joined: 06/25/2007
User offline. Last seen 2 years 1 week ago.

Hey guys,

I was wondering if there was a way to retain the .php extensions on a Drupal 6 site. I am converting a site that currently has pages ending in .php and they have a lot of external links to these pages.

I am using the path module to rename pages to match the existing site but of course if you try to navigate to one of those pages with .php at the end you get a 404 page.

I looked over the .htaccess file and didn't see an obvious fix for this. Any suggestions?

Rick
"For what will it profit a man if he gains the whole world, and loses his own soul?" (Mark 8:36 NKJV)

G&G Moderator
G&G Podcast Host
micah's picture
Joined: 06/21/2007
User offline. Last seen 2 weeks 3 days ago.
The dark and scary places of .htaccess and RewriteCond

Rick,

URL rewrite rules in .htaccess can be a very scary place. However, I'll bet if you can craft the correct rule to put into your file, you can force a rewrite that will strip the .php extension, allowing Drupal's normal rewrite rules to take over and finish the job. (Assuming your URL aliases would match the old paths, sans extension.)

I did a quick Google search of RewriteRule remove extension and found some examples, then tried to mash them into what Drupal already does by default.

I think something like this might make a good starting point for testing. I have not tested this, so it might not work without tweaking.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.php$ [NC]
RewriteRule ^(.*)\.php$ index.php?q=$1 [L,QSA]

This is basically the default rewrite string for Drupal 5.x, modified to strip the .php extension. Place it immediately above the rule that is already there, then test and edit until it works. (Please post your final answer here. I'm guessing the solution would be similar for sites migrating away from a .html extension into Drupal.)

Micah

Joined: 06/25/2007
User offline. Last seen 2 years 1 week ago.
Much Closer

Micah,

Thanks so much for looking at this. dark and scary was a good title for the day before Halloween!

I have replaced this line:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
with this:
RewriteRule ^(.*)\.php$ index.php?q=$1 [L,QSA]

I can now go to whatever.com/foo.php and that's great however going to whatever.com/foo redirects back to the index page.

I have some htaccess rewrite rules that I have used on my non-Drupal sites to allow users to go to my .php pages without the .php extension and I have been playing with them to try to achieve the opposite effect with Drupal. I haven't had any success yet but if I get it I will post it here.

Thanks!

Rick
"For what will it profit a man if he gains the whole world, and loses his own soul?" (Mark 8:36 NKJV)

G&G Moderator
G&G Podcast Host
micah's picture
Joined: 06/21/2007
User offline. Last seen 2 weeks 3 days ago.
Lines I suggested were add not replace

Rick,

The section I suggested were intended to be inserted, not to replace the default RewriteRule line. I'm thinking that if you simply replace it, that would break the normal rewrite functionality. That sounds a lot like what you're describing.

Micah

Joined: 06/25/2007
User offline. Last seen 2 years 1 week ago.
Same results

Sorry Micah I should have mentioned that both rewrite rules achieved the same results. I put the one you wrote in place and then modified it to the one I posted.

Because the .php extension comes before the node variable in the url string and the rewrite rule essentially removes the index.php?q part of the string I'm not sure if I can even achieve creating a rule that will let me get to the content both with or without the extension.

Thanks,
Rick

Rick
"For what will it profit a man if he gains the whole world, and loses his own soul?" (Mark 8:36 NKJV)

Joined: 06/25/2007
User offline. Last seen 2 years 1 week ago.
I'm almost there

Ok this Rewrite rule does exactly what I want. It allows a user to access my sites pages either with or without a .php extension. It essentially strips off the .php extension and then applies drupals url rewrite rule.

# Externally redirect client requests for URL.php to extension-less URL
   RewriteCond %{THE_REQUEST} ^(.*)\ /(.*)\.php
   RewriteRule ^(.*)\.php$ http://www.mysite.com/$1 [R=301,L]
  # Drupal Rewrite Rule
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

The only problem is that for some reason it breaks all links to stylesheets, images and all other content that is brought in from a file path. Anyone know why this is breaking relative links?

Thanks!

Rick
"For what will it profit a man if he gains the whole world, and loses his own soul?" (Mark 8:36 NKJV)

G&G Moderator
G&G Podcast Host
micah's picture
Joined: 06/21/2007
User offline. Last seen 2 weeks 3 days ago.
Need to add the other filters back in?

Rick,

I was going to suggest the 301 trick. Sounds like it worked for you.

Do you need to add these filters back in?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Those were part of the original Drupal rewrite, and filter make the last RewriteRule only happen if the REQUEST_FILENAME doesn't exist as a file or a directory. Also, without this check, it'll rewrite requests to index.php (and cron.php, update.php, etc.)

I tested the following, and it works.

  # Strip legacy extension from phony files.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{THE_REQUEST} ^(.*)\ /(.*)\.php
  RewriteRule ^(.*)\.php$ http://www.example.com/$1 [R=301,L]

  # Rewrite current-style URLs of the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

It also works with a .html extension, which could prove quite useful when migrating static sites.

Micah

Joined: 06/25/2007
User offline. Last seen 2 years 1 week ago.
Thank You!

Micah,

You're right, that was what I was missing! I have added in the extra rewrite conditions and it's working perfectly.

Thanks so much IOU Big Time!

Rick
"For what will it profit a man if he gains the whole world, and loses his own soul?" (Mark 8:36 NKJV)

G&G Moderator
Josiah's picture
Joined: 12/20/2007
User offline. Last seen 15 weeks 3 days ago.
I've achieved this in a small

I've achieved this in a small site I'm rebuilding by simply using the path module. I set the path to page.php and then in the browser you can go to domain.com/page.php and it just works. This solution doesn't scale well, but perhaps it'll work okay for you.