Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite: file and folder with same name

         

Ciuin

3:16 am on Mar 21, 2007 (gmt 0)

10+ Year Member



Hello,

I have a problem with my htaccess file. Maybe someone can help.

I have a number of files and folders with the same name:

/name.php
/name/
/othername.php
/othername/
etc.

and I want the URL [domain.com...] (without file
extension!) to point to the PHP-file, not to the directory.

I am trying

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ $1.php [L]

but get a 500 server error.
Renaming the folders or files is not an option.

Any ideas? Thank you.

jdMorgan

1:06 pm on Mar 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> but get a 500 server error

It would be helpful to know what information is logged in your server error log as a result of this error -- Very often, the information in that log will tell you exactly what is wrong.

Typical problems:

A space is required between "}" and "!" in both RewriteConds. Posting on this forum removes that space.

Some server configurations will require either both of these lines, or only the second line, before you can use mod_rewrite:


Options +FollowSymLinks
RewriteEngine on

Jim

Ciuin

6:23 pm on Mar 21, 2007 (gmt 0)

10+ Year Member



Hello Jim,

mod_rewrite does work on my server, and FollowSymLinks is on by default (therefore no need to turn it on in .htaccess). I did not give the full .htaccess file above, only the RewriteConds and RewriteRules that I was playing around with, since only these are causing me problems. Sorry, for not making that clear.

For my above problem, the following htaccess file (now given in full) works fine:

RewriteEngine on
RewriteBase /
RewriteRule ^name[/]?$ /name.php [L]
RewriteRule ^othername[/]?$ /othername.php [L]

i.e. if I specify each and every folder/file - without RewriteCond (!). But the problem with this is twofold: (a) obviously I will have to specify each folder/file, and that means hundreds of RewriteRules in my .htaccess file - which is not good for server performance (and a pain to keep up to date); and (b) this rule gives me an address of "http://www.domain.com/file" (with or without trailing slash, just as entered by the visitor), if there is no folder of the same name, but it always displays "http://www.domain.com/file/" (with a trailing slash), if there is a folder of this name, even if the visitor entered the address without slash in his browser location bar - the slash is added, if a folder of that name exists (which I don't want).

So, I am looking for a RewriteRule that will take care of all file/folder pairs, but if I try to redirect a call for the folder (".../name/") to the file rewritten without slash (".../name"), my server adds a slash (*) which again leads to the folder, and the result is an infinite loop.

So in fact the main problem seems to be:

How to keep my server from adding slashes to the end of addresses of folders - because the slash is not added, if I call a file that has no corresponding folder -, but at the same time it must add a slash to the end of "http://www.domain.com"?

jdMorgan

8:32 pm on Mar 21, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are trying to do something that's not recommended (confusing directory indexes with files) and you're in a fight with Apache mod_dir, which is trying to maintain orthodoxy. So the choices are to disable that module (comment out the Loadmodule directive for it), re-compile your server without it, or to make your RewriteRule a bit smarter:

# if requested url-path without trailing slash does not exist as a file
RewriteCond %{DOCUMENT_ROOT}/$1 !-f
# and if requested url-path plus a trailing slash does not exist as a directory
RewriteCond %{DOCUMENT_ROOT}/$1/ !-d
# rewrite to url-path without trailing slash, appending ".php"
RewriteRule ^(([^/]+/)*[^/]+)/?$ $1.php [L]

Note that with $1 in the RewriteConds, I'm back-referencing the requested URL stripped of its optional trailing slash that has matched the RewriteRule pattern. To resolve back-references to nested parentheses, count left parentheses; In this case, $1 is the entire requested local URL-path, and $2 is only the final path-part of that local URL-path, less the optional trailing slash in both instances.

The orthodox approach is to use trailing slashes only on URLs which refer to directory indexes, and to treat URLs without trailing slashes as files. A pair of rulesets could be used to do a 301 redirect to add a slash if any slashless URL exists as a directory index, and a 301 redirect to remove the slash if any slashed URL exists as a file. This would then leave you with a situation where your original internal rewrite code should work properly, as well as "correcting" your search engine listings.

Jim