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 6979725
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:55:21+00:00 2026-05-27T17:55:21+00:00

I have found some very useful code: RewriteEngine On RewriteCond %{HTTP_HOST} ^www.example.com$ [NC] RewriteRule

  • 0

I have found some very useful code:

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

from another Stack Overflow post:
Remove WWW prefix from your website.

I often re-use the same code on various sub-domains and sites so decided to rewrite a generalised version. I came up with the following:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

It appears to work, but can anyone give me a reason why I should not use this? Have I missed an obvious flaw?

Edit: I am aware that removing the www has associated issues, but in this case I am more interested in problems arising from writing a generalised htaccess RewriteRule (rather than the actions of the rule itself)

Edit: Code edited as recommended by @ulrich

  • 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-05-27T17:55:22+00:00Added an answer on May 27, 2026 at 5:55 pm

    I would also escape the. otherwise it would match www2.somedomain.com and your %1 would end up being .somedomain.com (if you had such a subdomain configured in the future)

    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    

    If you want to further limit it to just a .com you could use

    RewriteCond %{HTTP_HOST} ^www\.(.+\.com)$ [NC]
    

    or .net etc

    RewriteCond %{HTTP_HOST} ^www\.(.+\.(com|net|edu))$ [NC]
    

    You may also want to add a RewriteBase / after the RewriteEngine On in case you add other rules to your .htaccess later that need it.

    Barring those cases, it should work as is.

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

Sidebar

Related Questions

I have found some code on measuring execution time here http://www.dreamincode.net/forums/index.php?showtopic=24685 However, it does
I have found some code snippet in the internet. But it is missing some
Some days ago, I have found a useful link in which a day on
I have some legacy code that provides a list of the available COM ports
I have found some useful info on this issue but can't quite wrap my
Probably a very newbie question but, Ive been reading around and have found some
I'm new to custom objects but have found them very useful, especially because the
I have found some in the Cappuccino website (vim, textmate and SubEthaEdit), but not
I have found some libraries or web services in PHP that does the job.
I have found some info on the subject ( like this link) , but

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.