Forum Moderators: phranque

Message Too Old, No Replies

HTAccess redirect folders

Rewriterule 301

         

digic

10:07 am on Oct 7, 2012 (gmt 0)

10+ Year Member



I have unfinished issue from the old thread.
[webmasterworld.com...]

After years of modification, I am now bringing back old html files to a new subdomain. Before, these folders (with bunch of html) are in the root of the server like this:
http ://www .example.com/folder1/
http ://www .example.com/folder2/
http ://www .example.com/folder3/
Etc…

Now, I created a subdomain for them and name it ‘acrhives’ so my new structure would be…

http ://archives .example.com/folder1/
http ://archives .example.com/folder2/
http ://archives .example.com/folder2/
Etc…

After that, I modified the htaccess file adding the following code:

RewriteEngine on
#
RewriteRule ^folder1/(.*)$ http://archives.example.com/folder1/$1 [R=301,L]
RewriteRule ^folder2/(.*)$ http://archives.example.com/folder2/$1 [R=301,L]
RewriteRule ^folder3/(.*)$ http://archives.example.com/folder3/$1 [R=301,L]


So far, redirection is working fine, I just would like to seek advice from the Pro’s here if I am doing it right. Is there a negative effect for adding a bunch of RewriteRule?

Thanks

[edited by: incrediBILL at 9:31 pm (utc) on Oct 7, 2012]
[edit reason] Use Example.com per Forum Charter [/edit]

g1smd

10:16 am on Oct 7, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Looks good.

If 1 2 3 are literal, you can use

RewriteRule ^folder([123])/(.*)$ http://archives.example.com/folder$1/$2 [R=301,L]


Do look at the folder names to see if rules can be combined in a similar manner.

[edited by: incrediBILL at 9:32 pm (utc) on Oct 7, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

lucy24

11:10 am on Oct 7, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



... and even if 1-2-3 are not literal, if the old names ("folder1") and new names ("folder1") are really the same-- at least some of the time-- they can be merged as

RewriteRule ^(folder1|folder2|folder3)/(.*)$ http://www.example.com/$1/$2 [R=301,L]

or simply

RewriteRule ^((folder1|folder2|folder3)/.*)$ http://www.example.com/$1 [R=301,L]

Hm. "archives.example.com" doesn't get you much further does it? ;)


I made cookies with mod_rewrite today. Er, yesterday. My server did not explode and my logs did not turn into a solid mass of 503s. I am STOKED.

digic

11:53 am on Oct 7, 2012 (gmt 0)

10+ Year Member



g1smd and lucy24, many thanks for the quick response.

@lucy24, putting them on a subdomain is an experimental move to get back after being hit by Panda. I noticed that there are important backlinks pointing to these html file, so I am bringing it back (but noindex). It is my guess that shallow content on these pages is the cause of the penalty and after reading several threads here, moving them to a subdomain may help in the recovery.

Regarding my question. Yes, folders are all the same, its just like moving them to a subdomain from the root. So is it OK to merge them as:

RewriteRule ^((news|tutorial|others)/.*)$ http://archives.example.com/$1 [R=301,L]


And it gives these results...
http ://www. example.com/news/ to http ://archives. example.com/news/
http ://www. example.com/tutorial/ to http ://archives. example.com/tutoria/
http ://www. example.com/others/ to http ://archives. example.com/others/

One more thing, how about ALL .html from the root to the root of the subdoman like this one:
http: //www. example.com/article.html to http: //archives. example.com/article.html

[edited by: incrediBILL at 9:33 pm (utc) on Oct 7, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

lucy24

10:41 pm on Oct 7, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The subdirectory is physically located in a folder inside the top-level directory, right? So there's no way to create an htaccess that will only be "seen" by requests for the old name. That means you'll always have to check. (The opposite isn't true. If you want to intercept only requests for the new name, you can put a separate htaccess in that directory. But only do this if it's a choice between two htaccess files and an unspeakable mess ;))

Are you redirecting absolutely everything? If so, it turns into a simple variation on the conventional hostname redirect

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

where the ? part picks up correctly worded requests for directories. This rule should be preceded by the equally standard

RewriteCond %{THE_REQUEST} index\.html
RewriteRule ^(([^/.]+/)*)index\.html$ http:/ /archives.example.com/$1 [R=301,L]

to avoid a double redirect. Note that this element omits the HTTP_HOST line since you will be redirecting either way.

Your choice whether you also need/want to redirect requests for subsidiary files such as images. It's rare for anything other than a robot to ask for them "cold"; if the request comes after a page load, it will already be in the right form.

Make sure you don't accidentally redirect things like robots.txt or sitemap.xml that haven't moved. So either way, it's better to constrain your rules to specific extensions.

digic

8:57 am on Oct 9, 2012 (gmt 0)

10+ Year Member



Subdirectories and html files before are at root of public_html. With the creation of subdomain 'archives' all of them are moved inside public_html/archives/.

What was left on the top level directory (public_html) are components of my present CMS which is wordpress. So basically, ALL .html are now inside the 'archives' folder, that is all I want to redirect. There are no changes made on the structure of the old html site, except from moving them from public_html/ to public_html/archives.

phranque

10:34 am on Oct 9, 2012 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



so I am bringing it back (but noindex). It is my guess that shallow content on these pages is the cause of the penalty and after reading several threads here, moving them to a subdomain may help in the recovery.

how is noindexing the content going to help with panda recovery?

where the ? part picks up correctly worded requests for directories.

the ? Part in the HTTP_HOST test?
that's for HTTP/1.0 user agent requests which don't supply a hostname.

g1smd

7:02 pm on Oct 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



where the ? part picks up correctly worded requests for directories

The one in the RewriteRule, not the one in the RewriteCond. :)

lucy24

7:13 pm on Oct 9, 2012 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yup. With further complications if both the outer site (the root) and the inner site (the subdomain) include directories called by properly formatted name, meaning final slash. If they do, you'll need one or two extra rules to deal with them, and limit the rule discussed earlier to requests that explicitly say .html at the end.

If there's a manageable number of directories involved, you can just make a rule listing them by name.

digic

1:58 am on Oct 20, 2012 (gmt 0)

10+ Year Member



Hi, sorry. I'm on a leave for the past days. I'll do some tests based on your suggestions and see what will happen.

Thanks again.