Forum Moderators: phranque

Message Too Old, No Replies

.htaccess To Overide Higher Re-write Rule?

PHP Re-write rule

         

erika1959

6:35 am on Aug 29, 2004 (gmt 0)

10+ Year Member



I use mod_rewrite to write pages created dynamically from a basic .php file (keywords.php) to .html or .htm extensions.

To do this, my sites typically use the following .htaccess located in the root directory;

# -------------------------------------------------------------
# Redirect any .html file to /keywords.php

RewriteEngine on
Options +FollowSymLinks
RewriteRule \.html$ /keywords.php [L]

# -------------------------------------------------------------

I now find I need to create a different set of pages, and do to this had intended to create a number of sub-directories with their own .htaccess files which would (so I thought), over-write the root .htaccess file and use the content of their own keywords.php .

Unfortunately, it seems that lower .htaccess files don't overwrite higher ones if they're identical in structure.

To get around this, I experimented with the following alternatives to the original .htaccess file - the first trying to limit the command to the root directory only, the second specifically excluding a named sub-directory;

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI}!/.*/
RewriteRule \.html$ /keywords.php [L]

-----------------------------------------------------

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI}!/subfoldername/
RewriteRule \.html$ /keywords.php [L]

Neither alternative works - the root directory .htaccess continues to over-ride the lower ones.

Anyone got any suggestions as to how I can work around this?

Many thanks,

Erika.

jdMorgan

9:53 pm on Aug 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your last version (Request_URI!=subdirectory) should have worked. Flush your browser cache and try it again.

Jim

erika1959

7:29 am on Aug 30, 2004 (gmt 0)

10+ Year Member



Did as you suggested Jim - when I call pages from the sub-folder, I'm still getting pages based on the original .htaccess commands.

I tried changing the name of the file that's to be rewritten ("keywords.php") in the sub-folder) and adjusting the sub-folder's .htaccess file to reflect this. Let's say I call it "secondkeywords.php" .

Unfortunately, this just produces a 404 error, in which secondkeywords.php is "not found".

It seems as if the sub-folder .htaccess is being read, but then the parser is looking reverting to the root folder to find the required file - which of course isn't there.

I've temporarily solved the problem by placing secondkeywords.php in the root folder, and things seem to work OK, with both the root and sub-folder pages displaying, but I'd prefer to have things run a little more cleanly if possible, with files being called from the folder for which they're intended.

My guess is that I've missed something really small somwhere the path(s) on one or both of the .htaccess files, but I can't find it....

I've just noticed that there's a mistake in my original post, so here are the .htaccess files as I've installed them;

In the root folder:

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_URI}!/subfoldername/
RewriteRule \.html$ /keywords.php [L]

In the sub-folder:

Redirect any .html file to /secondkeywords.php
RewriteEngine on
Options +FollowSymLinks
RewriteRule \.html$ /secondkeywords.php [L]

Sorry if this all sounds confusing...

Regards,

Erika.

jdMorgan

5:04 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, it's always confusing, trying to describe directory structure and everything else in plain language. We just do the best we can...

Perhaps you have RewriteOptions inherit set. It is rare to have this set by default, but it would cause the problem you describe. See the mod_rewrite docs for info about inherit.

Jim

jdMorgan

5:12 pm on Aug 30, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or maybe it's just that the substitution URL should be prefixed with the sub-folder name:

In the sub-folder:


# Redirect any .html file to /secondkeywords.php
RewriteEngine on
Options +FollowSymLinks
RewriteRule \.html$ [b]/sub-folder_name[/b]/secondkeywords.php [L]

A leading slash on the substitution URL indicates that the server should start at Document_Root to resolve the URL-path, so the original version does in fact tell it to look for secondkeywords.php in the root folder.

Alternatively, in the sub-folder, you could use:


# Redirect any .html file to /secondkeywords.php
RewriteEngine on
Options +FollowSymLinks
RewriteRule \.html$ [b]s[/b]secondkeywords.php [L]

I prefer the first version, though, because it is unambiguous.

Jim