Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite with directories

         

Stewart81

2:42 pm on Jul 19, 2011 (gmt 0)

10+ Year Member



Hi,

I have been trying to overcome a problem with directories that were auto generated without having to move files around extensively using mod_rewrite. Basically I have say a directory named:

blah

And also a file named:

blah.php

Ideally blah.php should be index.php within the directory blah but for whatever reason in the process this didn't happen.

I came up with this:

RewriteCond %{SCRIPT_FILENAME}/index.php !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]

The idea is if I type in a directory path and index.php does not exist then pull up the file with the same name as the directory with the .php extension.

It works in the most basic sense. If I enter the URL of a directory without a forward slash at the end I get what I want.

The only problem I run into is if I add in a / at the end. Then it fails to pull it up. I believe it is failing because blah/.php does not exist but despite searching around a can't find any method for trimming the / off of the path before adding in the php extension. It may not even be possible and if it's not I will just have to find another way but I figured it may be possible and I'm hoping someone may have an idea that may at least put me in the right direction.

Thanks for the help

Stewart

lucy24

8:16 pm on Jul 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Urk. For starters: You mean that your actual file name is /blah/blah.php but it's functionally the index file, so you need to grab any unsuspecting user who types in /blah/index.something ?

Oh, wait a minute, I've actually got the identical configuration in one of my private directories.

:: detour to some other browsers to verify that it does what it's supposed to ::

Yes, hurrah. My htaccess doesn't even say anything about the trailing slash, so the server must have done that part before it got to htaccess.

RewriteRule silence/(index\.html?)?$ /silence/silence.html [R=301,L]

In your case since you're dealing with more than one directory, you replace the first occurrence of "silence" with (directory|otherdirectory|stillotherdirectory) etc. and replace the other two occurrences-- the ones in the target-- with $1. Yes, you are allowed to use the same $1 twice. You don't use $2, which is the (index\.html?)? part; if you are anxious you can put ?: inside the parentheses to keep it from getting captured at all.

g1smd

9:23 pm on Jul 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



While that works, it does mean that at least three different URL requests will deliver the same content. That's a Duplicate Content issue.

The resultant page should contain the rel="canonical" tag and that should announce the correct canonical URL for the content: one ending in trailing slash and NOT including a filename.

The alternative is to redirect non-canonical URL requests to the canonical URL using a 301 redirect. That is the best long term solution as non-canonical URLs never remain in the browser address bar.

lucy24

10:52 pm on Jul 19, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



While that works, it does mean that at least three different URL requests will deliver the same content. That's a Duplicate Content issue.

If they've never been to the other pages-- in this case for the iron-clad reason that they don't exist-- they've got no reason to suspect duplicate content, assuming they're sent to the right place via a duly recorded 301. Personally I don't mess with rewrites; it's always a Redirect, no matter what rule I use* to create it.


* Apache themselves [httpd.apache.org] say you should use mod_alias whenever you can. I'm not going to argue ;) even if it means having to remember when not to escape a .