redirect, rewrite, alias - which one?

hello.  I have a need to redirect a site, say to

I have a rewrite rule in place now to get the users from http to https
I also have a redirect in place now to get the users to the write somedir if they do not add it.
These work.

Various iterations trying to get them going from one subdomain to another is not working.  What am I missing?  thanks.
Who is Participating?
Erdinç Güngör ÇorbacıConnect With a Mentor PHP Development Team LeaderCommented:
You can use 3 methods for 301 redirection

- Using the PHP header() function if your webserver supports PHP.
- Using the mod_rewrite functionality of Apache servers.
- Using built-in forwarding if your host provides it.

For first one ;

add this to the beginnings of the pages at your old domain.  (better include from a file )

 if(stristr($_SERVER["HTTP_HOST"], 'www')){
  header("HTTP/1.1 301 Moved Permanently");
  header("Location:" . $_SERVER["REQUEST_URI"]);

Open in new window

code above sends the visitor from

as you can see not just domain but either page data is being used from url.
And exit() is important to make sure the rest of code is not executed before redirection.

Second way is using htaccess. -i prefer this one-

put lines below in a file named .htaccess (cant rename to this in windows rename after ftp if needed)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^myoldsite\.com [nc]
RewriteRule (.*)$1 [R=301,L]

if you need to carry Query Strings you can use QSA in [] at end of last line.

For more diggin you may consider taking a look at these pages ;

good luck :)

BTW : To test any 301 redirection, make sure you clear your browser's cache first! Otherwise, you won't see if it worked or not.
you must edit your httpd.conf file

Listen 80
NameVirtualHost *

<VirtualHost *>
DocumentRoot /home/httpd/htdocs/

<VirtualHost *>
DocumentRoot /home/httpd/htdocs/subdomain/

Open in new window

also setup a CName in dns records
Tyler LaczkoCommented:
you can use a JS or PHP script to do it.

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

bongiosAuthor Commented:
I have edited the httpd.conf but as I said, the results were not as expected.
I do not have access to dns records, but will suggest that do to the admin there.

Where would the JS or PHP script be placed for it to run?

thanks much all.
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Best way of doing this without losing Pagerank (if its important) is doing a 301 redirect.

Write me down if you need more help... bb
bongiosAuthor Commented:
I'd like more info on this, please.
Erdinç Güngör ÇorbacıPHP Development Team LeaderCommented:
Hi! Do you have any other  questions upon my last answer?
bongiosAuthor Commented:
have not yet implemented -- will try the 301 this weekend.  Thanks so much!
bongiosAuthor Commented:
I apologize for not awarding the solution. Extreme oversight on my part.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.