user1514316
user1514316

Reputation: 31

Codeigniter url rewrite like subdomain url with .htaccess

Currently my Url is: http://www.domain.co.uk/index.php/city/details/city-name

I would like to change it to:

http://www.city-name.domain.co.uk/index.php/city/details/city-name

or:

http://www.city-name.domain.co.uk/city/details/city-name

Upvotes: 0

Views: 1052

Answers (1)

mabarroso
mabarroso

Reputation: 659

Put the .htaccess file into the http ://www.domain.co.uk/ document root

To http ://www.city-name.domain.co.uk/index.php/city/details/city-name

RewriteRule ^(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]

To http ://www.city-name.domain.co.uk/index.php/city/details/city-name

RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L] 

If the server is the same, set above RewriteRule this line to prevent redirection loop

RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]  

File content example

<ifModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]  
   RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]  
</IfModule>

To exclude domain.co.uk (whitout www)

<ifModule mod_rewrite.c>
   RewriteEngine on
   RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
   RewriteCond %{HTTP_HOST} !^domain\.co\.uk [NC]  
   RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]     
</IfModule>

Upvotes: 1

Related Questions