Forum Moderators: phranque

Message Too Old, No Replies

Htaccess Rewrite Creating Unlimited Subdirectories

Need help.

         

ersane

3:37 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



I have a directory site using the script phplinkdirectory...

There are 2 level of subdirectories dir1/dir2

The problem is when i use mod rewrite dir1/dir2/dir1 is also valid and show the content of dir1.

This creates an endless amount of subdirectories like ;

games/entertainment/basketball/money/health/webmaster/chat/

which are supposed to be either dir1 or dir 2 level

I should have 4000 indexed pages on google but the current number is 145.000 . And this kills all my rankings..

The htaccess file is something like that :

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-l
RewriteRule ^games(.*) index.php
RewriteRule ^entertainment(.*) index.php
RewriteRule ^money(.*) index.php

Where i am making a mistake? How can i prevent this error?
Thanks...

jdMorgan

4:31 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To correct exactly what you've got, I'd suggest:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^games/[^/]+/?$ /index.php [L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^entertainment/[^/]+/?$ /index.php [L]
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^money/[^/]+/?$ /index.php [L]

although I deleted the "if not a link"-checking RewriteCond in the name of efficiency; Unless you're manually creating and using symbolic links, that checking is a waste of time.

However, you might ask yourself the question, "Do I have any URLs in the form "/dir1/dir2" that I *do not* wish to rewrite to index.php? If so, how many are there?

If the answer is "no", or if there are only a few URLs of that form that you don't want to rewrite, then you can dispense with the multiple RewriteConds and RewriteRules, and just use one rule:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/]+/[^/]+/?$ /index.php [L]

And you could even dispense with the slow/inefficient file-or-directory-exists checks and eliminate the exceptions mentioned above if you're willing to change your links to include a 'tag' that explicitly tells mod_rewrite that the URL is to be rewritten to index.php -- Something like:
/directory/dir1/dir2
-or-
/d/dir1/dir2/
In this second example case, the entire ruleset becomes a simple unconditional rule:

RewriteRule ^d/[^/]+/[^/]+/?$ /index.php [L]

without any need to search through all of your files and directories to see if the URL resolves to an existing file or directory. The 'explode' in your index.php script would have to be modified to throw away the /d/ part of the URL-path, but that should be a fairly simple modification.

Jim

ersane

6:44 pm on Sep 16, 2007 (gmt 0)

10+ Year Member



Thanks alot JdMorgan, it worked like a charm...

However, you might ask yourself the question, "Do I have any URLs in the form "/dir1/dir2" that I *do not* wish to rewrite to index.php? If so, how many are there?

I had a few real directories so my case was the first one you suggested.. I had to write a rule for each directory but afterall its working perfect..

I have been looking for a solution for a couple of weeks and i cant really tell how grateful i am...

jdMorgan

8:07 pm on Sep 16, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you have a limited number of directory-paths that match the rule pattern, but that you don't want to rewrite, simply exclude them:

RewriteCond $1 !^admin/stats
RewriteCond $1 !^cart/scripts
RewriteRule ^([^/]+/[^/]+)/?$ /index.php [L]

It all boils down to which you have more of, directory-paths that should be rewritten, or those that should not.

You could also avoid multiple rules for your current method like this:


RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(games¦entertainment¦money)/[^/]+/?$ /index.php [L]

There are many ways to do it, and it's a matter of finding the most efficient method that meets your needs.

Replace the broken pipe "¦" characters above with solid pipe characters before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 11:21 pm (utc) on Sep. 16, 2007]

g1smd

9:16 pm on Sep 16, 2007 (gmt 0)

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



I've bookmarked this thread. It answers so many questions in so few lines of code. :-)