Forum Moderators: phranque

Message Too Old, No Replies

redirect everything but index page?

         

Skier88

3:54 pm on Nov 1, 2010 (gmt 0)

10+ Year Member



I'm trying to direct all requests in a certain directory (/dir/) except for that directory's index (/dir/index.php) to a certain script (/dir/item.php). This sounds very simple even to me, but I've spent hours trying at least 20 different methods with no success.

I want to solve this with a .htaccess file in the scripted directory (/dir/.htaccess). Here's my latest attempt:

RewriteEngine On
RewriteRule !index\.php$ item.php

This works redirects everything but /dir/index.php; the problem is it also redirects /dir/ (which resolves to /dir/index.php), and I don't want to specify the index file.

I think the problem might have something to do with the higher level .htaccess (/.htaccess), so I'll post all of that too.

# ERROR DOCUMENTS
ErrorDocument 403 /_structure/403.html
ErrorDocument 404 /_structure/404.html

RewriteEngine on

# ACCESS CONTROLS
RewriteRule ^dir/login_vars\.php$ - [F]
RewriteRule (/|^)php\.ini$ - [F]
RewriteRule (/|^)\.(htaccess|htpasswd|htgroup)$ - [F]

# EXTERNAL REDIRECTS
RewriteCond %{HTTP_HOST} !^(mysite\.com)?$
RewriteRule ^(.*)$ http://mysite.com/$1 [R=301,L]

# INTERNAL REDIRECTS
RewriteRule ^search$ search.php
RewriteRule \.html$ _structure/page.php [NC]

Also, note that there are other files in /dir/, so I can't just redirect if it is not a file.

Any suggestions? Thanks for reading.

sublime1

3:52 pm on Nov 2, 2010 (gmt 0)

10+ Year Member



Why not keep the rewrites all in a single .htaccess? I think this will tend to simplify the problem, or at least simplify debugging.

So I am a little confused -- at first you say you want to direct all requests to /dir/ except the index.php there -- then you suggest you don't want this to apply to files in the directory.

I think some examples might help clarify the request.

Tom

Skier88

6:58 am on Nov 17, 2010 (gmt 0)

10+ Year Member



Thanks for the reply sublime1. I actually solved my problem, but I didn't want to leave this thread unconcluded.

You're right, the root of the problem was that I hadn't thought my requirements through entirely. The script's purpose is to wrap the content in a header and footer; I found that a much more straight forward solution is to only redirect .html files and manually include the wrapping files if a scripted page requires them. Apparently this works for non-specific index requests as well, since apache will redirect appropriate requests to an index.html file before it goes to .htaccess.

jdMorgan

10:36 pm on Nov 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Apparently this works for non-specific index requests as well, since apache will redirect appropriate requests to an index.html file before it goes to .htaccess.

It is actually Apache mod_dir which *internally rewrites* (not redirects) directory index URL requests (e.g. requests for "example.com/" or "example.com/mydir/") to the filepath specified in the DirectoryIndex directive(s) (e.g. local filepaths "/index.php" or "/mydir/main.html").

Jim

Skier88

1:55 am on Nov 18, 2010 (gmt 0)

10+ Year Member



Thanks for the clarification; and terminology is always useful.