g1smd

msg:4546117 | 1:07 pm on Feb 16, 2013 (gmt 0) |
URL ending with slash is for a folder. That's in the HTTP specs. Don't fight it. URL ending without slash can either be a page or can redirect to URL for folder. You should NOT allow both "with slash" and "without slash" URLs to directly display content. That's a Duplicate Content problem. Replace your final line with this:
RewriteRule ^(([^/]+/)*[^/.]+)$ /$1.php [L] and make sure you link to the "without slash" version of the URL from the other pages of the site. Note the other changes in this one line of code, especially the slash before the $1. That is vital to prevent hackers using path injection techniques against your site. You could add another rule to redirect "with slash" requests to "without slash". That new and extra rule would go before the rule that rewrites requests. Use example.com in this forum to prevent URL auto-linking. [edited by: g1smd at 1:27 pm (utc) on Feb 16, 2013]
|
jpm5

msg:4546119 | 1:21 pm on Feb 16, 2013 (gmt 0) |
Thanks! Can you tell me how to do that? This is my very first time with apache. I post my current code again as it seems to small to read. -------
IndexOptions FancyIndexing Options All RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php?
RewriteRule ^(([^/]+/)*[^.]+)\.php?$ www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f # then append ".htm" to resolve the actual filename RewriteRule ^(([^/]+/)*[^/.]+)$ /$1.php [L] [edited by: jpm5 at 1:44 pm (utc) on Feb 16, 2013]
|
g1smd

msg:4546120 | 1:28 pm on Feb 16, 2013 (gmt 0) |
Please edit the post above to - use example.com - add the changes posted in the previous post. Uge the [ code ] and [ /code ] tags around code if you want. The edit button is below your user name.
|
jpm5

msg:4546131 | 1:47 pm on Feb 16, 2013 (gmt 0) |
I updated the code. I'm afraid I still have the issue. Even if a type www.example.com/one it adds a slash and show the directory tree.
|
lucy24

msg:4546132 | 1:50 pm on Feb 16, 2013 (gmt 0) |
| I'm unable to load the file as the server understands I'm trying to access the directory |
| You need to intercept the request before mod_dir gets hold of it. If you're on shared hosting this may or may not be possible since you can't change the order of the modules. Option B is to turn off the directory-slash redirect. But that is better described as, uhm, Option Z because there are a lot of arguments against it. But if you're going extensionless, all your URLs will changing anyway, so why not simply rename the troublemaking file? btw I know exactly the situation you are in. I've got one pretty big directory whose content consists entirely of foobar.html foobar/ widget.html widget/ and so on. (The directories contain a bunch of stuff linked from the named file, but no index file.) Of course what I ought to do is reposition all those named files as foobar/index.html widget/index.html and so on (calm down, g1, I'm talking about physical filenames, not URLs ;)) but ugh, the search engines still haven't caught up to my last renaming from almost two years ago. It isn't clear from your first post whether you have a user-accessible file in the /widget/ position, or only the /widget.php file (the one that's changing into /widget alone)? If you have both, the only sane remedy is to rename one or the other.
|
jpm5

msg:4546134 | 1:56 pm on Feb 16, 2013 (gmt 0) |
I think I got it, I disabled the directory slash... final code working goes like this: DirectorySlash Off IndexOptions FancyIndexing Options All RewriteEngine on RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php? RewriteRule ^(([^/]+/)*[^.]+)\.php?$ www.example.com/$1 [R=301,L] RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(([^/]+/)*[^/.]+)$ /$1.php [L] ------------ It works! But what do you think? Is it ok or too messy? THANKS A LOT!
|
g1smd

msg:4546138 | 2:44 pm on Feb 16, 2013 (gmt 0) |
You're now conforming to the HTTP specs: - URL with slash is a folder or the index page in a folder. - URL without slash is a page. Remove the question mark after .php here (two places). The question mark makes the final p optional, redirecting requests ending in .php or in .ph and that isn't what you intended. Add a blank line after each RewriteRule. Remove blank lines after each RewriteCond. Add http:// to the rule target of the redirect. You must include both protocol and hostname in redirects.
|
lucy24

msg:4546234 | 11:19 pm on Feb 16, 2013 (gmt 0) |
DirectorySlash Off
IndexOptions FancyIndexing These two directives side by side make me VERY uneasy because they imply that you use auto-indexing, at least sometimes; FancyIndexing would otherwise be meaningless. From the horse's mouth [httpd.apache.org], where it is enclosed in a red box: | Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents. |
| This will not apply to the specific file that prompted you to switch off the Directory-Slash Redirect, since you're dealing with that separately. But it will apply to all other directories everywhere. Are you positive this is a better approach than renaming one file (which as already noted you will be doing anyway)?
|
|