Apache Web Server Forum

I need a way to add index.htm at end of url. For example: www.example.com/folder1/folder2 AND www.example.com/folder1/folder2/ should both point to www.example.com/folder1/folder2/index.htm (and preferable be redirected there with 301 redirect).

Folders do not exist physical. The /folder1/folder2 is the result of a rewrite rule:

What do you need all those conditions for? If you already know that the directories don't physically exist, there's no need to test for them.

RewriteRule ^([^./]+/[^/.]+)/?$ http://www.example.com/$1/ et cetera

I see what you're trying to do here

^(.*?)/?$

but it won't work. You want the RegEx to stop before the final slash, if there is one, and omit the slash from the capture. But Regular Expressions don't work that way.

How many of these nonexistent directories are there? It may be quicker to list them by name in your Rule.

But now for the real question:

Why on earth do you want to do this? If they were real files and directories, you'd be redirecting to get rid of the trailing "index.htm". If the directories don't exist, then their index.htm files don't either. You're just taking an extra detour before the final rewrite.

there more than 120 basic folders (subfolders may be well over 1000 in any combination of 2/3/4 level deep) so its out of the question to write that many rules

as for the regex... so far it seems to work in the project. No strange links, no double slashes, all appear well. example.com/folder1 AND example.com/folder1/ get properly rewritten into example.com/folder1/index.htm... i don't see why the regex will fail - but i have a very limited experience with htaccess/apache enviroment. Care to explain a little more?

i don't see why the regex will fail - but i have a very limited experience with htaccess/apache enviroment.

Same here, but this one is purely about Regular Expressions.

^(.*?)/?$ There are two pieces to this pattern: (.*?) "Capture the content, if any" /? "There might be a / at the end" Since the slash is not mandatory, the capture does not have to stop. It might, depending on RegEx dialect, but it doesn't have to.

I detoured to check in my text editor. There the pattern worked as intended. But I have met very similar situations where the .+? plus {blahblah}? pattern didn't do what I wanted it to. So I wouldn't rely on it. Remember that Regular Expressions, like computers as a whole, will misunderstand you whenever they possibly can. So you have to make a rule that leaves absolutely no wiggle room or space for ambiguity.

The one situation where a *? or +? construction will probably work as intended is if you had something like

^(.+?)/(.+)$ applied to a request containing more than one slash: dir1/dir2/dir3 You should then get (dir1)/(dir2/dir3) instead of the default (dir1/dir2)/(dir3)

Nothing wrong with extensions. It's only the specific filename "index.xtn" that's a problem. Ask your client if he wants requests for directory/ to be explicitly redirected (not just silently rewritten) to directory/index.htm

If he says "No, either way is fine", make up some scary stuff about Duplicate Content. If he says yes, give up and humor him.

ok after a small talk with the customer he agreed to a non-index.htm approach. so, the "new" approach must satisfies these conditions: - non-www into www urls - index.htm/index.html to / url - trailing slash to be added if needed

#SlashesWhen are slashes added?Trailing slash denotes a folder or the index page in a folder.The DirectorySlash directive takes care of that automatically.URLs for pages should not have a trailing slash. This is in the HTTP specs.