Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite to redirect to new directory

please help...urgent ..

         

HonHon

5:18 am on Nov 27, 2003 (gmt 0)

10+ Year Member



Help me! I need help on mod rewrite in .htaccess file.

Currently, I have a hundreds of shtml file sitting in [domain.com...] and few levels of sub directories. The contents were copied over to [domain.com...] , but some of the 3rd level and above directory structure have been change or removed.

I need to add in a mod rewrite in the .htaccess file to redirect all users accessing the old site to the new site of the same page they requesting from the old. But if the new site does not host the old site pages anymore, it should move up 1 directory level and look for index.shtml..

By the way, I only have access to www.domain.com/abc and www.domain.com/xyz, I do not have access to www.domain.com files.

below is the htaccess i using. Its place inside abc directory but its aren't working.

RewriteEngine On
RewriteRule ^([^/]+)/?(.*) /xyz/$1 [R=301,L]

jdMorgan

6:16 am on Nov 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Something like this might work:

In example.com/abc/.htaccess:


# Rewrite all requests for example.com/abc/<file> to example.com/xyz/<file>
RewriteRule ^(.*)$ /xyz/$1 [E=OldFwd:Yes]

In example.com/xyz/.htaccess:

# If this is a request forwarded from old 'abc' subdir, and the
# requested file is not (already) 'index.shtml', and the
# requested file does not exist, then rewrite to index.shtml
RewriteCond %{ENV:OldFwd} ^Yes$
RewriteCond %{REQUEST_URI} !^/index\.shtml$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.shtml$ /index.shtml [L]

Your problem is rather complex, and this may not work. Even if it does work, be aware that this approach is highly-inefficient. I would advise using it *only* as a temporary fix.

You also did not say whether you wanted the new-subdirectory URL to show in the browser's address bar, and this affects the design. I assumed that you did not want the new-subdirectory to show in the address bar, simply because it is more efficient to do an internal redirect (and it won't show in this case). Otherwise, you may need to do this:

In example.com/abc/.htaccess:


# Redirect all requests for example.com/abc/<file> to example.com/xyz/<file>
RewriteRule ^(.*)$ http://www.example.com/xyz/$1 [R=301,L]

In example.com/xyz/.htaccess:

# If requested file is not (already) 'index.shtml', and the
# requested file does not exist, then redirect to index.shtml
RewriteCond %{REQUEST_URI} !^/index\.shtml$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.shtml$ http://www.example.com/xyz/index.shtml [R=301,L]

This solution is even more inefficient than the first one, but it will show the correct URL in the browser address bar.

Some warnings:

1) You will lose the capability to detect missing files on the /xyz site -- You will never get a 404 on an .shtml file, because missing .shtml files will always be redirected to '/index.shtml'.

2) Search engine spiders may get very confused about which files are missing and which are not, for the same reason - they will never see a 404-Not Found server response, and will always get a copy of /index.shtml if they request a non-existent file.

For these reasons, you will be better off if you can avoid having very many missing files.

If you get errors, review your server error log - it often provides critical information needed to fix the problem.

Neither of these solutions may be exactly what you want; many, many details must be specified to get the perfect solution. Also, these solutions were not tested, and may contain typos. But... they were free. :)

Jim

HonHon

7:14 am on Nov 27, 2003 (gmt 0)

10+ Year Member



Hi Morgan,

Thanks for your reply ...

I forgot to mention, the URL is the new directory is suppose to be visible.

I did try using RewriteRule ^(.*)$ [example.com...] [R=301,L]

but if i type example.com/abc it give me example.com/xyz//opt/httpd...****xx,

if I will to type in example.com/abc/, it will direct me to example.com/xyz/

I figure out the only way i can make /abc and /abc/ to point to /xyz/ is by using: RewriteRule ^([^/]+)/?(.*) /xyz/$1 [R=301,L]

But now i do have another porblem, it seem like directory like www.example.com/abc/dir1/dir2/index.shtml will be directed to www.example.com/xyz/dir1/index.shtml .. It does not go further than 2nd level.

jdMorgan

9:00 am on Nov 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The simplest way to get past that two-level problem is to use two rules:

RewriteRule ^([^/]+)/(.*) /xyz/$1/$2 [R=301,L]
RewriteRule ^([^/]*) /xyz/$1 [R=301,L]

Note that each one must have the same RewriteConds as above - you will have to duplicate them to get it working.

Jim