Jump to content

Recommended Posts

Posted (edited)

(please, note domain1 & sub1 are given as an example)

I've created subdomain named "sub1" in domain "domain1"

i want that whenever i access "http://sub1.domain1/index.php",'>http://sub1.domain1/index.php",

it should render "http://domain1/index.php?subdomain=sub1"

but, in browser, the 1st link i.e. "http://sub1.domain1/index.php" should be displayed..

i've tried following code in .htaccess file for index.php file:::

 

 

RewriteEngine on

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{HTTP_HOST} ^domain1$ [OR]

RewriteCond %{HTTP_HOST} ^www.domain1$ [OR]

RewriteCond %{HTTP_HOST} ^sub1.domain1$

RewriteRule ^(.*) http://domain1/index.php?subdomain=$1 [L,QSA,R]

 

it works sometimes...n thn not..

what should i do..?

Edited by sneh_nsk
Posted

Welcome to the forums, sneh_nsk!

 

Honestly, I'd be surprised if it worked at all. :)

 

>RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

These conditions are testing if the page name requested is both not a file and not a directory, which is impossible if the page really exists.

 

>RewriteCond %{HTTP_HOST} ^domain1$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain1$ [OR]

You said you want a rewrite rule that works on sub1.domain1, but you're testing for domain1 and www.domain1 here and allowing rewrite processing to continue even if it is not your subdomain.

 

>RewriteCond %{HTTP_HOST} ^sub1.domain1$
RewriteRule ^(.*) http://domain1/index.php?subdomain=$1 [L,QSA,R]

In the rewrite rule, you want the $1 in '?subdomain=$1' to be placed with 'sub1'. As the subdomain name is not a part of the page name that the rewrite rule tests (what the ^(.*) in RewriteRule sees), you can't use a $1 backreference to get the subdomain name. As it is, if someone requested 'http://sub1.domain1/index.php', the rewrite rule will see and capture 'index.php', and you'd end up with a new URL of 'http://domain1/index.php?subdomain=index.php'.

 

If you want the original URL to remain in the browser, you can't use the 'R' flag in the RewriteRule. This causes the browser to do a forced redirect to the new URL and guarantees that the new URL will be shown in the browser's address bar instead of the original URL.

 

I'd suggest something like this to start with:

>RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub1.domain1$
RewriteRule ^(.*) index.php?subdomain=sub1 [L,QSA]

You may need to modify this, depending on your needs, as any page requested under sub1.domain1 will display the main domain1's index.php page.

 

Hope this helps...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...