Nigel Waters
Nigel Waters

Reputation: 145

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

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]

Upvotes: 1

Views: 411

Answers (1)

anubhava
anubhava

Reputation: 786031

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]

Upvotes: 2

Related Questions