homepage Welcome to WebmasterWorld Guest from 54.196.62.23
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
htaccess, drop extension, file/folder same name
jpm5



 
Msg#: 4546106 posted 12:24 pm on Feb 16, 2013 (gmt 0)

Hi!

I want to drop file extensions but there's a problem as I have a file and a directory with the same name. I'm unable to load the file as the server understands I'm trying to access the directory and shows the tree.

This is a code I found in this site, but it doesn't work but I can't find what it's wrong with it. It drops the extensions but when accessing to file it shows the directory tree.

EXAMPLE

www.domain.com/product/product | works fine!
www.domain.com/product/ | shows directory tree instead of www.domain.com/product.php (without the extension of course)


IndexOptions FancyIndexing
Options All
RewriteEngine on


# Externally redirect client requests contains htm/html extension to extensionless URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php?

# externally redirect to extensionless URI
RewriteRule ^(([^/]+/)*[^.]+)\.php?$ http://www.example.com/$1 [R=301,L]





# and if requested extensionless URL-path plus ".htm" does resolve to an existing file

RewriteCond %{REQUEST_FILENAME}.php -f
# then append ".htm" to resolve the actual filename
RewriteRule ^(([^/]+/)*[^/]+)/?$ $1.php [L]


THANK YOU!

[edited by: jpm5 at 1:37 pm (utc) on Feb 16, 2013]

 

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4546106 posted 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#: 4546106 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4546106 posted 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#: 4546106 posted 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

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4546106 posted 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#: 4546106 posted 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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4546106 posted 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

WebmasterWorld Senior Member lucy24 us a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



 
Msg#: 4546106 posted 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)?

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved