Forum Moderators: phranque

Message Too Old, No Replies

folder redirection

mod rewrite folder redirection

         

henrybabel

3:00 am on May 17, 2007 (gmt 0)

10+ Year Member



hey guys,

i was wondering if someone can help me with a folder redirection.
i have this:

# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI}!^(/Projects/)?$
# Rewrite all those to insert /projects.
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*)$ /projects/$1 [R,L]

Cause we recently had /Projects/ and /projects/ and we merged all the sub-folders together into the lower p, /projects/.
We want anyone that goes to /Projects/anything to be taken to /projects/anything - so we have a seemless upgrade to the site.

at the moment, that just writes:
www.mydomain.com/projects/projects/projects/projects/projects/Projects/myfolder/page3.html

jdMorgan

3:07 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing in the code to stop that from happening -- mod_rewrite is executed recursively in .htaccess until no more 'actions' result. This is mainly to be sure that a newly-rewritten URL-path is checked for authentication/authorization and access restrictions.

Two ways to fix it:
1)


# Only apply to URLs that aren't already under /Projects or /projects folder.
RewriteCond %{REQUEST_URI} [b]!^(/[Pp]r[/b]ojects/)?$
# Rewrite those that do not exist in requested path to insert the /projects path.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /projects/$1 [L]

2)

# Only apply this rule one time per HTTP request
RewriteCond %{ENV:Rewrite_Done} !^True$ [NC]
# Only apply to URLs that aren't already under /Projects folder.
RewriteCond %{REQUEST_URI} !^(/Projects/)?$
# Rewrite those that do not exist in requested path to insert the /projects path.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /projects/$1 [E=Rewrite_Done:True,L]

These use two completely-different methods to prevent the rule from being applied repeatedly. The first is a problem-specific solution, while the second is more general. Note that the "Rewrite_Done" variable name is arbitrary -- Call it whatever you like, as long as your variable name does not conflict with a named server variable.

Note that it may be that the case of "Projects" is wrong in your original code. I wasn't sure from your description, so I'm assuming a less-simple problem, though.

Jim

henrybabel

4:56 pm on May 17, 2007 (gmt 0)

10+ Year Member



the first idea isn't working - it's creating:
www.domain.com/project/Project/traincrossing/traincrossing.html

what about something like:

RewriteCond %{REQUEST_URI}!^[P]rojects/$
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^.]+)\.([^/]+[^.]+)$http://%{HTTP_HOST}/$1/$2/$3/$4\.$5 [L]
RewriteRule ^([^/]+)/([^/]+)/([^.]+)\.([^/]+[^.]+)$ [%{HTTP_HOST}...] [L]
RewriteRule ^([^/]+)/([^.]+)\.([^/]+[^.]+)$ [%{HTTP_HOST}...] [L]
RewriteRule ^([^.]+)\.([^/]+[^.]+)$ [%{HTTP_HOST}...] [L]

so far i can't get this to work.
i need to take care of any extension, .html, .shtml, .jpg, .zip, etc
and i need to take care of no more than 4 folders after /Projects/

jdMorgan

9:19 pm on May 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> the first idea isn't working - it's creating:
> www.domain.com/project/Project/traincrossing/traincrossing.html

Sorry, missed the end-anchor on the first RewriteCond:


# Only apply to URLs that aren't already under /Projects or /projects folder, and are not "/".
RewriteCond %{REQUEST_URI} !^/([Pp]rojects/¦/$)
# Rewrite those that do not exist in requested path to insert the /projects path.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /projects/$1 [L]

Replace the broken pipe "¦" character in the RewriteCond pattern with a solid pipe before use; Posting on this forum modifies the pipe characters.

Jim

[edited by: jdMorgan at 9:20 pm (utc) on May 17, 2007]

henrybabel

10:56 am on May 29, 2007 (gmt 0)

10+ Year Member



hey - sorry for the late reply - i'm still having problems with these lines of code.
it's still sending to Projects/projects/projects/projects/projects/

i am using the .htaccess in my top parent folder, .com/.htaccess
i don't want to create the .htaccess in the old folder /Projects - we don't want it to be there, to stop ourselves from posting files into this folder, when we only want to use the lower case, /projects/

Anyone see what the problem with the code is?

jdMorgan

2:44 am on May 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please confirm this warning:
> Replace the broken pipe "¦" character in the RewriteCond pattern with a solid pipe before use; Posting on this forum modifies the pipe characters.

Also, it looks like there may be a problem with handling the "/" file, so try this tweaked RewriteCond:


# Only apply to URLs that aren't already under /Projects or /projects folder, and are not "/".
RewriteCond %{REQUEST_URI} [b]!^(/[[/b]Pp]rojects/¦/$)
# Rewrite those that do not exist in requested path to insert the /projects path.
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule (.*) /projects/$1 [L]

Jim

[edited by: jdMorgan at 2:49 am (utc) on May 30, 2007]