Generic 301 redirect htaccess

Hi all,

I'm looking for a good way to manage my URLs.

So what I currently have:

http://www.firstdomain.com/ (main domain) operating out of /public_html/ on my server.
http://www.seconddomain.com/ (addon domain) operating out of /public_html/seconddomain.com/

How can I write some .htaccess code so that:

http://www.firstdomain.com/ operates from /public_html/firstdomain.com/
http://www.seconddomain.com/ operates from /public_html/seconddomain.com/

Also I want the www versions of the sites to redirect to the non-www versions:

http://www.firstdomain.com/ redirects to http://firstdomain.com/
http://www.seconddomain.com/ redirects to http://seconddomain.com/

and finally for the first domain how do I get it to pass the requests to a php file:

the request for http://firstdomain.com/some/page/on-my-site
will make a call to http://firstdomain.com/index.php?page=some/page/on-my-site
and this would have to exclude image and css files.

Any help on the above would be fantastic!!

Thanks for your help in advance.

GW.
GhostWerxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cmaohioSenior Systems ManagerCommented:
While it requires much more research on my part to put it in this post quickly for you. One question I have is do you have mod_rewrite on the server? if you do, all of this can be accomplished with mod_rewrite. although since this is windows it may not do it but it's apache so it may. heh.

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
0
cmaohioSenior Systems ManagerCommented:
i guess it was another post I read them having windows with apache. Sorry about that.

ether way, to test if you have mod_rewrite. Create a simple file named "info.php" or something and put only this line:

<?php phpinfo(); ?>

then view it in your browser under the apache section it should say whether or not it's in there. Delete the file when you're done.
0
GhostWerxAuthor Commented:
Hi cmaohio,

I do have mod_rewrite enabled.

Is what I want to achieve possible and am I choosing to go about it the right way??

Are you able to help me out or point me to some good examples??

Thanks for your help in advance.

GW.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

cmaohioSenior Systems ManagerCommented:
You probably will have to do a couple of .htaccess files with mod_rewrite rules. Because your main would probably look like this:

RewriteEngine On
#Looks for www.firstdomain.com and redirects with a 301 redirect to it without the www.
RewriteCond %{REMOTE_HOST}  ^www\.firstdomain\.com
RewriteRule ^/(.*)    http://firstdomain.com/$1 [L,R=301]

#Looks for www.seconddomain.com and redirects with a 301 redirect to it without the www.
RewriteCond %{REMOTE_HOST}  ^www\. seconddomain\.com
RewriteRule ^/(.*)    http://seconddomain.com/$1 [L,R=301]

#Looks for first domain and tells it to pull files from the sub directory. Now, if the .htaccess file is
#in the public_html folder then, you will remove the "public_html/" from below.
RewriteCond %{REMOTE_HOST}  ^firstdomain\.com
RewriteRule ^/(.*)      public_html/firstdomain.com/($1) [L]

#the difference between the two above and the one at the bottom is the one at the bottom serves from a different location whereas the others do a redirect (R=301). the "L" directive tells it to stop reading more rules.
0
cmaohioSenior Systems ManagerCommented:
Ack! I have an error above.

See these instead:
RewriteEngine On
#Looks for www.firstdomain.com and redirects with a 301 redirect to it without the www.
RewriteCond %{REMOTE_HOST}  ^www\.firstdomain\.com
RewriteRule ^(.*)    http://firstdomain.com/$1 [L,R=301]
 
#Looks for www.seconddomain.com and redirects with a 301 redirect to it without the www.
RewriteCond %{REMOTE_HOST}  ^www\. seconddomain\.com
RewriteRule ^(.*)    http://seconddomain.com/$1 [L,R=301]
 
#Looks for first domain and tells it to pull files from the sub directory. Now, if the .htaccess file is
#in the public_html folder then, you will remove the "public_html/" from below.
RewriteCond %{REMOTE_HOST}  ^firstdomain\.com
RewriteRule ^(.*)      public_html/firstdomain.com/($1) [L]

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cmaohioSenior Systems ManagerCommented:
I wish there was an edit button! Notice the "second domain" section above. there is an inadvertant space there, didn't see that before the last two times. make that ^www\.seconddomain\.com  (no space)
0
cmaohioSenior Systems ManagerCommented:
Your last one is a bit more involved.  first, I would put the rules within the sub-directory and not in the main .htaccess file (thus having one for each domain)

You could do it two different ways.

1. Add a ReWriteCond looking in the positive for your filename extensions like:

RewriteCond %{SCRIPT_FILENAME} ^*\.php [OR]
RewriteCond %{SCRIPT_FILENAME} ^*\.html [OR]
RewriteCond %{SCRIPT_FILENAME} ^*\.htm [OR]
RewriteCond %{SCRIPT_FILENAME} ^*\.txt

2. add rewritecond looking in the negative for image extensions.
RewriteCond %{SCRIPT_FILENAME} !^*\.jpg [AND]
RewriteCond %{SCRIPT_FILENAME} !^*\.jpeg [AND]
RewriteCond %{SCRIPT_FILENAME} !^*\.css [AND]
RewriteCond %{SCRIPT_FILENAME} !^*\.gif

I recommend the former.

Then after the conditions. do:

RewriteRule ^(.*) index.php?page=$1

now I haven't tested this at all so please read the helpful link to apache above. And play.
0
GhostWerxAuthor Commented:
Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Miscellaneous

From novice to tech pro — start learning today.