Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8764407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T16:01:33+00:00 2026-06-13T16:01:33+00:00

According to: Daily Blog Tips The code below does two things: 1) It will

  • 0

According to: Daily Blog Tips

The code below does two things:

1) It will add (or remove) the www. prefixes to all the pages inside your domain.

2) The code below redirects the visitors of the http://domain.com version to http://www.domain.com.

My question: Is inserting the code below in the index.php page sufficient to create a 301 redirect that will work on all pages of the website I am working on?

<?php
if (substr($_SERVER['HTTP_HOST'],0,3) != 'www') {
header('HTTP/1.1 301 Moved Permanently');
header('Location: http://www.'.$_SERVER['HTTP_HOST']
.$_SERVER['REQUEST_URI']);
}
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T16:01:34+00:00Added an answer on June 13, 2026 at 4:01 pm

    My question: Is inserting the code below in the index.php page sufficient to create a 301 redirect that will work on all pages of the website I am working on?

    No. That will only work for http://www.example.com/index.php and not for http://www.example.com/whatever/whatever/file.php. You’re better off using Apache mod_rewrite to accomplish this task. You could stick this in your document root /.htaccess file.

    # Rewrite "example.com -> www.example.com".
    <IfModule mod_rewrite.c>
      Options +FollowSymlinks
      RewriteEngine On
      RewriteCond %{HTTPS} !=on
      RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
      RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </IfModule>
    

    Code was taken from HTML5 Boilerplate’s .htaccess

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to two sources ( http://bytes.com/topic/c-sharp/answers/224314-serial-communication-dll and http://www.innovatic.dk/knowledg/SerialCOM/SerialCOM.htm ), .NET 1.1 has no Serial
My application requires a re-start at midnight time daily. For this job, according to
According to PHP.net manual, pg_pconnect will create a persistent connection, or will return the
According MSDN, FxCop is an application that analyzes managed code assemblies (code that targets
I want to create a counter, daily, weekly and all time. Each product in
How does something like Statistically Improbable Phrases work? According to amazon: Amazon.com's Statistically Improbable
According to the mustache RFC A {{name}} tag in a basic template will try
According to https://issues.sonatype.org/browse/OSSRH-3293 Maven m2e Code-Style and Save-Actions connector This plugin is executed inside
According to CUDA streams not overlapping , the profiler will serialize streaming to get
According to the answer to this question , merging two hashes in Perl 5

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.