htaccess subdomain redirect


I've searched around and tried a few different ways but can't get this to work.

I don't actually have the subdomains set up, but will have about 30 similar redirects that I need to do.


sub-domain.domain.co.uk needs to go to <a href="http://www.domain.com/folder/page-name" rel="nofollow">http://www.domain.com/folder/page-name</a>

There will be hyphens in almost every subdomain and every 'page-name', so I'm not sure if this affects the regex formatting.

Any help would be much appreciated!



You need to use the %{HTTP_HOST} variable and match it in a RewriteCond, then use a %1 backreference to access it in a rewrite rule.

Searching for "subdomain redirect" on Stack Overflow, it looks like this is exactly what you want: <a href="https://stackoverflow.com/questions/961963/internal-subdomain-to-folder-redirect" rel="nofollow">Internal subdomain to folder redirect</a>

<hr />


The accepted answer in that StackOverflow link above:

RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{HTTP_HOST} ^([^/.]+)\.example\.com$ RewriteRule ^ %1%{REQUEST_URI} [L]

In your .htaccess file, put that above whatever rules you have. Obviously, you're going to need to modify example\.com to whatever your domain is, and have the proper directories setup in your document root.


  • Unable to Extract XML value from Oracle CBLOB
  • Remove duplicates entries from multiple text file in perl?
  • htaccess internal rewrite from subdomain to subdir path
  • htaccess Redirect if query string variable present
  • htaccess force www to non-www with consideration of http or https
  • mod_rewrite seems to ignore [L] flag
  • Laravel 5.2 Pretty URLs
  • dyanmic url not rewriting ht access
  • attr_accessor causes Rspec tests to fail
  • Add rewrite rule
  • Remove two subdirectories from url with mod_rewrite
  • Finding the last specific character type in a string
  • CodeIgniter and htaccess - Protect a specific controller and its methods via AuthType?
  • url rewrite not working .htaccess
  • Strip down any parameter from URL which is not whitelisted
  • Use htaccess to mask domain and folder name
  • python argparse add_argument_group required
  • Yii - Make a string usable in a URL or filename
  • Eclipse code fomatting
  • How to determine location of a perl warning - Use of each() on hash after insertion
  • Ansible syntax best practice, YAML dictionary (key: value) or equal sign (key=value)?
  • .htaccess rewrite: subdomain as GET parameter and filepath afterdomain intact
  • How to read xlsx file in an android application?
  • What's the point of nonfinal singleton objects in scala?
  • Do custom action after devise model confirmation
  • Formatting with mako
  • htaccess settings for angularJS pushstate
  • Is a .txt file created in VB different than one I'd randomly create?
  • Redirect to trailng slash (htaccess)
  • Is there a difference between initializing a variable in the default constructor or inline? [duplica
  • Save website uploads in a subdomain
  • Negating Regex PO BOX
  • Efficient User-Agent Regex to find Safari in Python
  • Keep this build forever option - Jenkins
  • Extracting HTML between tags
  • MongoDB in PHP using aggregate to group by _id is null not working
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Projection media query: browser support and workarounds?
  • Regex thinks I'm nesting, but I'm not
  • Different response to non-authenticated users and AJAX calls