How to I add a wildcard subdomain redirect to a subdirectory in htaccess?

不羁的心 提交于 2020-01-14 05:05:13

问题


My old subdomain urls look something like:

www.newyork.website.co.uk

I want to redirect it now to:

www.website.com/city/newyork

However I have over 2,000 cities and just want to add a wildcard redirect. How do I go about doing this in my htaccess file?

e.g. www.*.website.co.uk redirects to www.website.co.uk/city/*

THis is so far over myhead but this is what I have tried to muddle together:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain\.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/city/%{REQUEST_URI} [R=301,L]

回答1:


You can use this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(?:www\.)?((?!www\.)[^.]+)\.(website\.co\.uk)$ [NC]
RewriteRule ^/?$ http://www.%2/city/%1 [R=302,L]


来源:https://stackoverflow.com/questions/31301272/how-to-i-add-a-wildcard-subdomain-redirect-to-a-subdirectory-in-htaccess

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