htaccess subdomain redirect

天涯浪子 提交于 2019-12-25 02:43:34

问题


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.



来源:https://stackoverflow.com/questions/8195458/htaccess-subdomain-redirect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!