homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

writing a correct 301
regards a dir from a site moving to another site

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

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)

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)

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.


 11:52 pm on Dec 9, 2013 (gmt 0)

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)

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)

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)

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)

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.

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)

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)

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.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved