0

I have the following code in my .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /annsenglishmediumschool
    RewriteCond %{HTTP_HOST} shops.annsenglishmediumschool.com
    RewriteRule .* http://annsenglishmediumschool.com/index.php/shops [L]    
</IfModule>

This works fine , but how can i make "shops" as a variable here ? means whatever we type in the place of "shops" should come after http://annsenglishmediumschool.com/index.php/ thanks in advance .

EDIT : after edit my .htaccess looks like this :

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /annsenglishmediumschool
    RewriteCond %{HTTP_HOST} ^(.+?)\.annsenglishmediumschool\.com$
    RewriteRule .* http://annsenglishmediumschool.com/index.php/%1 [L]
</IfModule>

The problem is that it now takes only 'www' to the right as annsenglishmediumschool.com/index.php/www ,and no other string ,any help is greatly thanked

KTM
  • 858
  • 4
  • 21
  • 43
  • How is CodeIgniter related to this? – PurkkaKoodari May 07 '13 at 11:05
  • IN codeigniter i want to catch that variable types before the sitename ,thats what i mean ,thats is (anystring).mysite.com to mysite.com/mycontroller/myaction/(anystring) – KTM May 07 '13 at 13:05
  • Well, this is `.htaccess`, the problem is here, and your CodeIgniter PHP part is (probably) working. You were wondering what you should put in the _.htaccess_, not the _PHP_ to get this working. – PurkkaKoodari May 07 '13 at 14:16

2 Answers2

1

You can capture the part of the domain name you're interested in as %1 and use it in the RewriteRule

RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(.+?)\.annsenglishmediumschool\.com$
RewriteRule .* http://annsenglishmediumschool.com/index.php/%1 [R,L]

If the host does not start with www., this grabs the first part of the domain (.+?) and puts it in the RewriteRule as %1.

When everything works as you expect, you can change R to R=301.

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • when i types www.annsenglishmediumschool.com its taking the right as : annsenglishmediumschool.com/index.php/www and no other string is taking as when i type shops.annsenglishmediumschool.com its not going as such to annsenglishmediumschool.com/index.php/shops.This is the problem – KTM May 08 '13 at 05:54
  • Any solutions .i got stuck – KTM May 08 '13 at 10:57
  • @Icecreamsandwich Do you want to see the new URL in the browser bar? Or do you want to keep the original URL and just serve some different content? – Olaf Dietsche May 08 '13 at 12:16
  • I want to see the the new URL in the browser bar – KTM May 08 '13 at 12:22
  • @Icecreamsandwich Then you must not only rewrite but also redirect `R`. – Olaf Dietsche May 08 '13 at 12:30
  • but the problem is ,here its redirecting www also ,and not taking any other strings before sitename .thats when typing www.sitename.com its going to sitename.com/index.php/www – KTM May 08 '13 at 13:28
  • @Icecreamsandwich Do you want to exclude www.sitename.com from being rewritten? What do you mean with "not taking any other strings before sitename"? Can you provide an example and what happens when you enter this example, e.g. some error message or something else? – Olaf Dietsche May 08 '13 at 13:36
  • I mean that if i type :www.sitename.com its going to sitename.com/index.php/www ,its not that i want , i needed it as when type : subdomain.sitename.com then it should go to sitename.com/index.php/subdmain – KTM May 09 '13 at 04:30
  • @Icecreamsandwich Then you must exclude `www.` with a `RewriteCond`. The rule already does the subdomain part. Please see updated answer. – Olaf Dietsche May 09 '13 at 10:21
  • okay ,let me check ,but it was not going as explected : when i type subdomain.domain.com its not going to domain.com/index.php/subdomain ,it shows website not found – KTM May 09 '13 at 10:51
  • @Icecreamsandwich Then your server is not setup to serve `subdomain.domain.com`. The domain name must point to your webserver and the main config or virtual host config must have an appropriate [`ServerName`](http://httpd.apache.org/docs/current/mod/core.html#servername) or [`ServerAlias`](http://httpd.apache.org/docs/current/mod/core.html#serveralias). – Olaf Dietsche May 09 '13 at 12:52
  • How to set up as "The domain name must point to your webserver", i dont know ,any help is appreciated,the manual creation of subdomains in cpanel wont work – KTM May 09 '13 at 13:55
0

I think you are mixing two things. You can pass parameter directly to your controller to get the "shop" as variable.

for example :

index($variable){

echo $variable;

}

//It will echo $variable(shop this time) at yoursite.com/index.php/shop

akshayb
  • 1,219
  • 2
  • 18
  • 44
  • but its like subdomain , if user types subdomain.mysite.com then i want to catch the subdomain name in my controller ,now its static and accepts only "shops" – KTM May 07 '13 at 13:02