Apache Server configuration support - mod_rewrite and removing file extensions

This is Interesting: Free IT Magazines  
Home > Archive > Apache Server configuration support > August 2006 > mod_rewrite and removing file extensions





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author mod_rewrite and removing file extensions
Will McCutchen

2006-08-01, 7:28 pm

Hi all,

I'm not sure if this is an appropriate question for this group. If
not, please let me know.

I'm building a pretty simple web site. A sample of the directory
structure looks like this:


/about.php
/about/
/about/crimewatch.php
/about/definition.php
/about/ordinance.pdf
/about/stonepark.php

/archives.php
/archives/
/archives/minutes.php
/archives/newsletters.php

/contacts.php

/directions.php
/directions/
/directions/khcd-map.pdf


I would like to use mod_rewrite to remove the ".php" extension. I'm
new to mod_rewrite, and I'm completely at a loss about how to
accomplish this. A complication seems to be the fact that I've got
directories with the same name as some of the php files.

At the beginning, before I had an subdirectories with matching names, I
had these extremely simple mod_rewrite rules in my .htaccess:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1\.php [L]

Can anyone give me any help/tips/pointers?

Thanks,


Will.

Davide Bianchi

2006-08-01, 7:28 pm

On 2006-08-01, Will McCutchen <mccutchen@gmail.com> wrote:
> /about.php
> /about/


As rule of thumb, try to avoid having files and directories with the same
name: especially if you are using multiviews.

> I would like to use mod_rewrite to remove the ".php" extension.


And then how can poor apache knows when to process the files with PHP?

> RewriteBase /
> RewriteCond %{REQUEST_FILENAME}.php -f
> RewriteRule (.*) $1\.php [L]


This does the opposite: it ADD php extensions where not presents.

You could use a RewriteCond to test if the requested filename is NOT a
directory:

RewriteCond %{REQUEST_FILENAME} ! -d

and then apply your rule.

NOTE: I haven't tested it.

Davide

--
If Bill Gates is the Devil then Linus Torvalds must be the Messiah.
Will McCutchen

2006-08-01, 7:28 pm

Davide Bianchi wrote:
> As rule of thumb, try to avoid having files and directories with the same
> name: especially if you are using multiviews.


Yeah, I was afraid I was making a mistake...

>
> And then how can poor apache knows when to process the files with PHP?
>
>
> This does the opposite: it ADD php extensions where not presents.


I was not clear enough in my original post. I want mod_rewrite to ADD
the .php extension internally. I would like the external URI to not
end in .php. I would like a request to

/contacts

to be internally redirected to

/contacts.php

The simple .htaccess bit that I posted did accomplish that for me,
before I added directories with the same name.

I guess I may just have to shuffle my directory structure...

Thanks for your help,


Will.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2009 webservertalk.com