Welcome to WebmasterWorld Guest from 107.20.75.63

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

writing a correct 301

regards a dir from a site moving to another site

     
9:02 pm on Dec 9, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4388
votes: 2


I am setting a new site, and from a previously existing site I am going to remove a dir for example www.mysite.com/dir1
the whole content of that dir1 will be added to a new site
so how am I writing that 301 on my htaccess?
new location will be www.mysite2.com/dir1
I haven't done anything in that range since quite a long time and am more than rusty.
10:24 pm on Dec 9, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12714
votes: 244


Do the two sites live in the same place? That is: will requests for both sites pass through the same htaccess? RewriteRules will only be seen if they are located in an htaccess file visible to the old domain.

Second question: Are other directories staying at the old site? I assume yes, because otherwise it would be a non-issue: simply capture and redirect everything.

:: insert boilerplate about only-because-you've-been-around-since-2003 ::

If both domains pass through the same htaccess AND some directories are staying where they were:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule ^directory(?:/(.*))?$ http://www.example.com/$1 [R=301,L]

where example.com is the new domain. That slightly convoluted format is to cover requests for /directory without trailing slash. In addition to type-ins, some legitimate robots routinely do this.

If requests for the old domain DON'T pass through the new domain's htaccess, you don't need the condition. Unless the old location holds other domains that happen to have a top-level directory with the same name; then you would need a Condition that says

RewriteCond %{HTTP_HOST} example\.org

where example.org is the old domain.

All of this is assuming they are each full domains, not subdomains of each other.

My personal bias is to give each site its own htaccess file with its own RewriteRules, saving the shared htaccess file for things like "Deny from..." directives that apply to everyone-- AND that are inherited without complications. This may not be practicable if you're on the type of hosting setup that uses a "primary" domain and "addon" domains.
10:39 pm on Dec 9, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4388
votes: 2


yes, it will be two separated sites and two .htaccess
but I intend to del from the existing site that dir
then reinstalling it to the new site.
this is where I am hesitating in my next step.
Thanks.
11:52 pm on Dec 9, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12714
votes: 244


If the redirect in htaccess is correctly written, it will make absolutely no difference whether the directory still exists in its old location, because nobody will ever be able to reach it there. (Not even you, unless you code an exemption for yourself!)
12:30 am on Dec 10, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4388
votes: 2


Thanks, I remember, say way back when a few years ago you first came around asking questions.
since I mostly quit coding, but somehow came back, just for my own and using my php knowledge (what's left of it!) to make a WP working for me.
I knew you progressed while I regressed :)
5:58 am on Dec 10, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12714
votes: 244


Yes, if I continue at my current blazing pace of learning one word of php per year, I'll be fluent in no time :)
4:09 pm on Dec 11, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2003
posts: 4388
votes: 2


How does that look,
I want to 301 redirect the directory exists_dir/
from site_aa to site_bb


Note: I voluntarily removed the "p" from http as WebmasterWorld makes a ready to go link out of the address.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^site_aa.com[nc]
RewriteRule ^(.*)$ htt://www.site_aa.com/$1 [r=301,nc]

//301 Redirect Entire Directory
RedirectMatch 301 htt://www.site_aa.com/exists_dir/(.*) htt://www.site_bb.com/exists_dir//$1

[edited by: phranque at 7:31 pm (utc) on Dec 11, 2013]
[edit reason] unlinked url [/edit]

7:46 pm on Dec 11, 2013 (gmt 0)

Administrator

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Aug 10, 2004
posts:10544
votes: 8


RewriteRule ^(.*)$ htt://www.site_aa.com/$1 [r=301,nc]

//301 Redirect Entire Directory
RedirectMatch 301 htt://www.site_aa.com/exists_dir/(.*) htt://www.site_bb.com/exists_dir//$1


- this RedirectMatch regular expression won't match the protocol and hostname in the requested url.

http://httpd.apache.org/docs/current/mod/mod_alias.html#redirectmatch
The supplied regular expression is matched against the URL-path


- you shouldn't mix mod_alias and mod_rewrite directives within the same configuration.

http://httpd.apache.org/docs/current/rewrite/avoid.html [httpd.apache.org]:
when there are Redirect(Match) and RewriteRule directives in the same scope, the RewriteRule directives will run first, regardless of the order of appearance in the configuration file.
9:26 pm on Dec 11, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12714
votes: 244


WebmasterWorld makes a ready to go link out of the address

You can prevent this from happening by using the exact name "example.com". Or example.org or example.net or example.xyz, as needed. Any tld, but it has to be "example".
1:22 am on Dec 17, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 0


Make sure your new redirect is listed before the non-www to www redirect.

Use RewriteRule for all of the redirects.

Update the code for the non-www to www redirect using the code in the second post in this thread.