htaccess subdomain redirect

2019-08-25 03:20发布

问题:

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.

E.g:

sub-domain.domain.co.uk needs to go to http://www.domain.com/folder/page-name

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!

Thanks

回答1:

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: Internal subdomain to folder redirect


EDIT

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.