Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite

         

DaneM

6:15 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



Is it possible to add a mod_rewrite command so that....

one.mydomain.com
two.mydomain.com
three.mydomain.com

ALL point to [mydomain.com...]

Basically i want to create "fake" subdomains using mod_rewrite and have them point to the dir of [mydomain.com...] so that the subdomains read the content in the main dir.

Is this possible?

Thanks a lot.

jdMorgan

6:20 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, it's quite possible using code previously posted in this forum with only a few minor changes. However, before searching for those threads, I highly recommend that you do a search for "duplicate content penalty" here on WebmasterWorld.

In short, it's easy to do what you're asking about, but there is little potential benefit and much risk.

Jim

DaneM

6:21 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



How would be the best way to tackle this problem then?

jdMorgan

6:31 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That depends entirely on what end-goal you are trying to accomplish, and also possibly on why you think you want to accomplish it. Please provide more information of what you are actually trying to do, before we start discussing implementation details.

Jim

DaneM

6:43 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



basically i am creating a website.... which runs by getting the subdomain name as a variable...

[ONE.mydomain.com...]

ONE is got using PHP, same for if ONE was called TWO, php would get whatever the subdomain was and turn it into a variable.

Basically im using just one set of code in the main directy of the website, but i was subdomain to points to them so the code in the main dir gets the right data from the database.

For example

ONE.mydomin.com will get data from database

`one_news`

and TWO would pull

`two_news`

jdMorgan

7:03 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From a old thread, [webmasterworld.com...] :

RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^mail\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com
RewriteCond %{REQUEST_URI} !^/mydata/
RewriteRule (.*) /mydata/%1/$1 [L]

You need to change this to point all subdomain requests (except "www" and possibly a few others) to the same script, instead of pointing them to subdirectories. Luckily, this means the code can be simplified:

RewriteEngine on
# Redirect subdomain requests to /your_script.php - except for 'www.' and 'mail.'
RewriteCond $1 !^your_script\.php$
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^mail\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com
RewriteRule (.*) /your_script.php?subdomain=%1&URL_path=$1 [QSA,L]

If the script does not require the subdomain and URL-path to be passed to it as variables, then you can further simplify:

RewriteEngine on
# Redirect subdomain requests to /your_script.php - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^mail\.
RewriteCond %{HTTP_HOST} ^[^.]+\.example\.com
RewriteRule !^your_script\.php$ /your_script.php [L]

Jim

DaneM

8:25 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



I cant seem to get any of them working. on 1 of them i get a 500 error. grr

jdMorgan

8:30 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What's in your server error log?

Jim

DaneM

9:10 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



I cant seem to find it.

All i wanna do is create a fake subdomain lol. grrr

DaneM

9:11 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



ah found something

[Sat Nov 17 20:21:09 2007] [alert] [client *********] /home/fhlinux184/d/example.co.uk/user/htdocs/.htaccess: RewriteCond: bad argument line '%{REQUEST_URI}!^/one/'\n

[edited by: jdMorgan at 9:27 pm (utc) on Nov. 17, 2007]
[edit reason] example.com [/edit]

DaneM

9:28 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST}!^mail\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.co.uk
RewriteCond %{REQUEST_URL}!^/one/
RewriteRule (.*) /one/%1/$1 [L]

thats what im using.

one.example.co.uk brings up nothing

[edited by: jdMorgan at 9:30 pm (utc) on Nov. 17, 2007]
[edit reason] examplified per Terms of Service. [/edit]

jdMorgan

9:30 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The variable name must be REQUEST_URI, not REQUEST_URL

Jim

jdMorgan

9:32 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, note that a space is required between all "}" and "!" characters; Posting on this forum removes that space unless steps are taken to prevent it.

Jim

DaneM

9:51 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



this is what i have

RewriteEngine On
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST}!^mail\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.co.uk
RewriteCond %{REQUEST_URL}!^/one/
RewriteRule (.*) /one/%1/$1 [L]

and its redirecting me to

http://search.live.com/results.aspxFORM=DNSAS&q=one.example.co.uk

when i goto one.example.co.uk

[edited by: jdMorgan at 10:06 pm (utc) on Nov. 17, 2007]
[edit reason] example.com [/edit]

DaneM

9:52 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



i have also added spaces after the "!" but still no luck

DaneM

9:55 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



and going to [one.example.co.uk...] doesnt display anything

[edited by: jdMorgan at 9:59 pm (utc) on Nov. 17, 2007]
[edit reason] example.com [/edit]

DaneM

9:56 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



sorry for spam but cannot edit posts. error i now get with the code above is

/home/fhlinux184/d/example.co.uk/user/htdocs/.htaccess: RewriteCond: bad flag delimiters

[edited by: jdMorgan at 10:07 pm (utc) on Nov. 17, 2007]
[edit reason] example.com [/edit]

jdMorgan

10:03 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Generally, that's caused by a stray space character where it does not belong, or a problem with the syntax of the flags on RewriteConds or RewriteRules, such as the [L] or [NC] flags.

Understand that mod_rewrite code must be exactly, precisely correct to the very last character; The parser is very simple, efficient, and fast, and therefore, utterly unforgiving. mod_rewrite is not a high-level programming/scripting language; It does not allow wide variations in 'style'.

If you are making changes to the code, we need to see the code. Otherwise, we're just guessing.

Please use example.com or example.co.uk to avoid problems for both WebmasterWorld and for your site.

Also, it appears that you've ignored several of the previous errors I have pointed out. Perhaps a top-to-bottom review of this thread would help you.

Jim

[edited by: jdMorgan at 10:08 pm (utc) on Nov. 17, 2007]

DaneM

10:07 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



RewriteEngine On
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST}! ^www\.
RewriteCond %{HTTP_HOST}! ^mail\.
RewriteCond %{HTTP_HOST} ^(.+)\.example\.co.uk
RewriteCond %{REQUEST_URL}! ^/one/
RewriteRule (.*) /one/%1/$1 [L]

this is the mod_rewrite im using and its giving the 500 error.

and in my error log i get

RewriteCond: bad flag delimiters

DaneM

10:12 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



there are spaces between "!" and "}" on mine

jdMorgan

10:14 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Several errors. Copy and past this:

RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^mail\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.co\uk
RewriteCond %{REQUEST_URI} !^/one/
RewriteRule (.*) /one/%1/$1 [L]

Warranty expires immediately any character is changed... :)

Be aware that you will get a 404 error if /one/<subdomain>/<requested_URL-path> does not exist. You have changed the target URL-path in the RewriteRule so that it no longer rewrites to a script, but have not mentioned why you did this.

Jim

DaneM

10:20 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



i did copied and pasted it.

i dont get an error however when i goto

one.example.com

nothing comes up. it should be pointing to www.example.com?

the root dir?

jdMorgan

10:33 pm on Nov 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, it should be pointing to whatever file you want that URL to serve, or to the script you mentioned, but don't seem to want to acknowledge any more. Once inside your server, there are no domains or subdomains, only files.

Jim

DaneM

10:38 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



Im not sure i understand.

RewriteEngine on
# Redirect subdomain 'whatever' to subdirectory /'whatever' - except for 'www.' and 'mail.'
RewriteCond %{HTTP_HOST}!^www\.
RewriteCond %{HTTP_HOST}!^mail\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.co\uk
RewriteCond %{REQUEST_URI}!^/one/
RewriteRule (.*) /one/%1/$1 [L]

so one.example.co.uk will not point to www.example.co.uk

thats what i need lol

DaneM

11:17 pm on Nov 17, 2007 (gmt 0)

10+ Year Member



/www
1.php
2.php
3.php
4.php
5.php
6.php

# tables in the database would be
`teamname_news`
`teamname_members`
`teamname1_news`
`teamname1_members`
`teamname2_news`
`teamname2_members`

Going to teamname.mydomain.com would use the code in the /www dir and extract
`teamname_news`
`teamname_members`
because of the subdomain being teamname.

If the subdomain was teamname1.mydomain.com it would extract
`teamname1_news`
`teamname1_members`
because of the subdomain being teamname1.

But it is still using the same peice of code from /www

How would this be done?

jdMorgan

12:13 am on Nov 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> same piece of code from /www

What is the filename of this piece of code?

Jim