Forum Moderators: phranque
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.
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
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"?
# 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]
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