Forum Moderators: phranque

Message Too Old, No Replies

How To .htaccess redirect >> domain and sub folders puzzle

         

bookmarc

10:00 pm on May 18, 2010 (gmt 0)

10+ Year Member



Hello everyone,

My 1st time around here. *handshake*

I had two questions... it would great if someone would help.

Question 1)

I wanted to do a example.com 301 redirect to www.example.com . I looked around and found two sets of solutions...

SET 1)
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

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

I also found a lot of web sites suggesting
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] instead of [R=301,NC]

I am confused which one of these to use and why? I have had real bad experience with htaccess and i wanna be as careful as I can be.



Question 2)

I wanted to 301 redirect an entire contents of a folder (and sub-folder) and files to new domain

domain.com/folder-to-new-domain/files to >> new-domain.com/files

domain.com/folder-to-new-domain/folder/files to >> new-domain.com/folder/files
domain.com/folder-to-new-domain/folder/folder/files to >> new-domain.com/folder/folder/files

etc etc

How do i do this with .htaccess?
Given the number of folder and files involved I would like to put individual htaccess files in each folder instead of one big htaccess file. is that possible? (i do not want to touch the htaccess file in the main public_html folder it will break my whole website)

Thanks in advance. :)

BookMarc

[edited by: jdMorgan at 11:28 pm (utc) on May 18, 2010]
[edit reason] please use example.com only [/edit]

jdMorgan

12:19 am on May 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



SET 1)
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC]

Incorrect. Un-escaped literal periods, [NC] flag used on non-alphabetic-character pattern in RewriteRule, missing [L] flag on rule.

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

Incorrect. Does not allow for blank HTTP_HOST (creating an infinite loop), [NC] flag used on non-alphabetic-character pattern in RewriteRule, missing [L] flag on rule.

RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301] instead of [R=301,NC]

Fine as far as it goes. Order of flags is not significant.

The above should point out the problem you're heading into... You cannot make decisions about what code is appropriate to your site and whether it is correct without understanding what the code does and how it does it.

Further, this leads to the sentiment expressed in the latter part of your post:
i do not want to touch the htaccess file in the main public_html folder it will break my whole website.

This is a rather poor reason to determine the architecture of your rewrites and redirects. In fact, in many circumstances, you will not have the option to "not touch the main .htaccess file," as the rule order across all config files and .htaccess files affects how they work; There are plenty of things you just cannot do without touching the main .htaccess file, and any attempt to do them that way may lead to disaster -- either operationally, or as regards your search engine listings and rankings.

Your code needs to be absolutely perfect -- for your site. Only you really know your own site. Therefore, the decision that the code is perfect is one that you have to make. So you need the knowledge to make that decision.

So I'd like to refer you to our Forum Charter, where in addition to a description of how to get the most from this forum, there are also citations of several very-useful resources. A few hours spent with those resources (and the tutorials and example threads in our Apache Library) may help you to avoid some rather-catastrophically-bad code you'll find out on the Web, and allow you to have more confidence in the code that you write and/or select to control the operation of your server. (See the Charter and Library links at the top of this page.)

Jim