Forum Moderators: phranque

Message Too Old, No Replies

redirect www.abc.com to www.abc.com/newsite

but not /newsite/admin

         

tanjeff

1:03 pm on Apr 28, 2007 (gmt 0)

10+ Year Member



Hi all,

Hope that i can get some help here. I just created a new site www.abc.com/newsite and would like to redirect all links from old website to the new one. i.e www.abc.com to www.abc.com/newsite. By referring to another thread i got it working [sort of like ctrl c ctrl v thing] but only the admin directory in www.abc.com/newsite/admin also being redirected to www.abc.com/newsite/

To simplify the scenario:
(1) Redirect all from www.abc.com --> www.abc.com/newsite/
(2) Except robots.txt located in public_html
(3) Except www.abc.com/newsite/admin directory should not be redirected.

The structure of my website is as follow:

public_html <-- www.abc.com
-- robots.txt
-- .htaccess
-- images
-- all files and pages for old site
-- NEW FOLDER for NEW SITE <-- www.abc.com/newsite
------ images
------ all files and pages for new site

Please refer to my .htaccess file as follow:

RewriteEngine on
RewriteBase /

RewriteCond %{HTTP_HOST}!^abc\.com
RewriteCond %{REQUEST_URI}!^/robots\.txt
RewriteRule (.*) [abc.com...] [R=301,L]

Thanks in advance to everyone up here.

jdMorgan

7:03 pm on Apr 28, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are problems with this on multiple levels.

To answer your question as stated and as I understand it, you'd need to do this:


RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteCond $1 !^newsite/
RewriteCond $1 !^robots\.txt$
RewriteCond $1 !^admin/
RewriteRule (.*) http://www.example.com/newsite/index.php [R=301,L]

I question whether you really want or need to redirect your images, CSS, and everything else to a php file, but this is apparently what you want to do...

But a fundamental question is, why are you redirecting these requests, instead of rewriting them? By redirecting them, you give the visitor a complicated URL, expose the internal workings of your server, and require all the search engines to throw away your old URLs and start over. An internal rewrite simply substitutes the new content for the old content using the same old URL.

In example.com/.htaccess:


# Externally redirect example.com to www.example.com
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite all requests except /robots.txt and /admin to /newsite subdirectory
RewriteCond $1 !^newsite/
RewriteCond $1 !^robots\.txt$
RewriteCond $1 !^admin/
RewriteRule (.*) /newsite/$1 [L]

Then in example.com/newsite/.htaccess:

# Internally rewrite all /newsite requests except images, css, & js to /newsite/index.php
RewriteCond $1 !\.(gif¦jpe?g¦png¦css¦js)$
RewriteCond $1 !^index\.php$
RewriteRule ^(.*)$ /newsite/index.php [L]

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

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

tanjeff

2:28 am on Apr 29, 2007 (gmt 0)

10+ Year Member



Hi, thanks Jim great info there but what's the difference between redirect and rewrite, code wise i mean. Thanks.

jdMorgan

3:33 am on Apr 29, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> ...code-wise...

Please see the comments in the code posted above...

Jim

phranque

3:48 am on Apr 29, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



code wise it's the [R=301,L] that stops rule processing and forces the external permanent redirect (meaning the browser makes the new request).