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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:42:19+00:00 2026-06-13T11:42:19+00:00

I am looking for If, else type of code which can combine my re-write

  • 0

I am looking for If, else type of code which can combine my re-write rules based on host/domain name. So if domain name is “domainA” then redirect a to b, c to d and if domain name is “domainB” then redirect x to y and z to p. Basically I don’t want to write the Condition again and again as I have done below:

I have written following code for htaccess

    RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$
    RewriteRule ^home /? [L,R=301]

    RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$
    RewriteRule ^home-old /? [L,R=301]

In above I am using host because I have multiple domains pointed to same hosting space so htaccess is common between all.

Can I combine the above multiple condition for a domain into one, instead of writing domai name again and again for some set of redirects for specific domain?

Please advise, thanks!

  • 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-13T11:42:20+00:00Added an answer on June 13, 2026 at 11:42 am

    There really is no such if-then-else structure in mod_rewrite. You have to either be clever with your rules (which sometimes makes them unreadable or impossible to amend) or just be explicit about everything.

    For your specific example, you can just use regular expressions to combine them:

    ^domain\.com$ and ^www\.domain\.com$ gets turned into ^(www\.)?domain\.com$

    and

    ^home and ^home-old is just ^home, since the first only matches if the URI starts with home, and home-old does indeed start with home (there is no $ symbol to indicate the end of the string). So you’re looking at:

    RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
    RewriteRule ^home /? [L,R=301]
    

    If you need to be specific by using the $, you can just change the regex to:

    RewriteRule ^home(-old)?$ /? [L,R=301]
    

    EDIT:

    If I have some other urls instead of home, and botique, and hairtips then I need to write RewriteCond every time? I guess I have to write that every time just confirming from you

    Yes, you have to repeat the condition every time, or, you can make a passthrough at the very beginning of your rules:

    RewriteCond %{HTTP_HOST} !^(www\.)?domain\.com$ [NC]
    RewriteRule ^ - [L]
    

    This means: anything that’s not a request for host: http://www.domain.com or domain.com, then pass through without rewriting. Then you can just have rules after this because only requests with hosts other than the above will reach those rules.

    This is the “clever” bit that I was referring to before. This can make it tricky to change your rules or amend them later because you’ve set a strict condition at the top.

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

Sidebar

Related Questions

I am looking for a way in which I can grab an element id
Looking at some assembly code for x86_64 on my Mac, I see the following
Looking at some of the code System.Linq I've come across some examples of Buffer<TSource>
Looking for a way to get path of file (and then process it in
I have a gridview which can be filtered from one or more values in
I'm looking for an extension method or any other suggestion that can help me
I'm experimenting with Application.AddMessageFilter , using some code written originally by Somebody Else, so
I'm looking for best practices regarding abstracting my RIA domain context away from my
Looking to do a bit of refactoring... Using NHibernate I have this query currently
Looking for a perl one-liner what will find all words with the next pattern:

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.