homepage Welcome to WebmasterWorld Guest from 107.21.163.227
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Modrewrite subdomains to folder / dirrent folder for subdirectory
rewrite all exept www subdomain and directory on subdomain
ikbenhet4




msg:4421554
 3:22 am on Feb 25, 2012 (gmt 0)

Hello,
Formerly i came here under ikbenhet1, but i cannot seem to find my registration adres.
I want to internally rewrite the following rules, Rewrite Goal:

example.com to http://www.example.com/
*.example.com to /shops/ ( no path required, i handle path with php $_SERVER['REDIRECT_URL'])
*.examples.com/images/ to /images/$1 <- so images will be displayed

So what i have up til now, probably worth nothing:
RewriteCond %{HTTP_HOST} !^www\.webzjop\.info$ [NC]
RewriteCond %{HTTP_HOST} !^webzjop\.info$ [NC]
RewriteRule ^(.*)$ /shops/index.php [NC]

and then in the /shops/ direcory i have put:
RewriteEngine on
RewriteRule ^images/(.*)$ /images/$1 [L] <<-- this line is not working properly, it just redirects to /shops/
RewriteRule ^product/(.*)$ /shop/index.php [nc]
RewriteRule ^categorie/(.*)$ /shop/index.php [nc]
RewriteRule ^custom/(.*)$ /shop/index.php [nc]

Probably bad code, but it works , but i cannot make the subdomains *.example.com/images/ point to /images/

Best regards,
Ugur

 

lucy24




msg:4421565
 3:54 am on Feb 25, 2012 (gmt 0)

But what does

/images/

mean?

You don't say anything about a RewriteBase, and you don't include a protocol-plus-domain, so you're just redirecting back to the same place. In fact I'm surprised your browser doesn't step in and put up a "This is going nowhere fast" message.

RewriteCond %{HTTP_HOST} !^www\.webzjop\.info$ [NC]
RewriteCond %{HTTP_HOST} !^webzjop\.info$ [NC]

"If the host is neither 'www.webzjop.info' nor 'webzjop.info' then execute the rule."

What else could the host be?

ikbenhet4




msg:4421587
 5:13 am on Feb 25, 2012 (gmt 0)

Hi Lucy,

"In fact I'm surprised your browser doesn't step in and put up a "This is going nowhere fast" message."
These are not 301 or 302 redirects. These are internal redirects as the url in the adress bar does not change.
The "user home directory" for this user "web0272" is defined "/home/content/10/9013010/html"
"/" will refer to the base directory for that user meaning in this case "/home/content/10/9013010/html"

What else could the host be?
It will be the unique hostname of the user that registers it; for ex. If i sell for example computers; i could register the alias "computer" - any my webshop will be on this url: [computer.webzjop.info...] (the link is an working example)

But what does /images/ mean?
It means, rewrite all subdomains, starting with directory /images/$ to /home/content/10/9013010/html/images/$1

I will trie to be short and clear with examples:

all subdomains exept "www" must internally point to /home/content/10/9013010/html/shops/index.php
Covered with .htaccess in root:
RewriteCond %{HTTP_HOST} !^www\.webzjop\.info$ [NC]
RewriteCond %{HTTP_HOST} !^webzjop\.info$ [NC]
RewriteRule ^(.*)$ /shops/index.php [NC]


All images, for example exampleshop.webzjop.info/images/1/image.jpg must internally point to /home/content/10/9013010/html/images/1/image.jpg.
This line in .htaccess in shops one not working as expected in shops directory:
RewriteRule ^images/(.*)$ /images/$1 [L]

where exampleshop could be any host, exept www or nohost (webzjop.info)
The .haccess rewrite code works for everything, exept for the image rewrite

Best regards,
Ugur

ikbenhet4




msg:4421593
 6:11 am on Feb 25, 2012 (gmt 0)

Some testing further, i have narrow it down to:

RewriteRule ^images1/$ /images/ [nc]

works for computer.example.com/images1/, until these lines get added in .htaccess under it:

RewriteCond %{HTTP_HOST} !^www\.webzjop\.info$ [NC]
RewriteCond %{HTTP_HOST} !^webzjop\.info$ [NC]
RewriteRule ^(.*)$ /shops/index.php [NC]


after that it looks like the rewrite is giving priority to
the hostname rewrite, but the image rewrite is above it?

So i think i need to exclude /image/ directory from the hostname rewrite..

Any suggestions in the meanwhile?

Best regards,
Ugur

ikbenhet4




msg:4421601
 6:36 am on Feb 25, 2012 (gmt 0)

Im out of luck;

RewriteCond %{REQUEST_URI} !^/images1/
RewriteCond %{HTTP_HOST} !^www\.webzjop\.info$ [NC]
RewriteCond %{HTTP_HOST} !^webzjop\.info$ [NC]
RewriteRule ^(.*)$ /shops/index.php [NC]

does not prevent images1 to be excluded from the rewrite.

Hope someone knows what i am dooing wrong.
Thanks.
Beste regards,
Ugur

lucy24




msg:4421608
 7:29 am on Feb 25, 2012 (gmt 0)

does not prevent images1 to be excluded from the rewrite.

That's because it is no longer /images1/; it has been rewritten to /images/ You're looking at {REQUEST_URI} not {THE_REQUEST}. They are not always the same thing.

Your /images/ rewrite does not have an [L] after it, so mod_rewrite continues.

Put the [L] flag at the end of every Rewrite, unless you absolutely unambiguously know what you are doing. (The flags [F] [G] and [P] automatiocally mean [L]. But [R] does not, so be extra careful.)

Most seriously: I kinda doubt that you want to rewrite all requests to /shops/index.php. What you probably want to do is rewrite pages-- requests ending in .php or .html or whatever users see. Otherwise you will find very bizarre things happening with your images and stylesheets.

:: now backtracking to figure out how many domains you've got and what happens where ::

g1smd




msg:4421611
 8:00 am on Feb 25, 2012 (gmt 0)

If you put all rules in the root htaccess file you will have more control over which order they are parsed.

Add the [L] flag to every rule and order the rules from most specific to most general.

ikbenhet4




msg:4421617
 8:35 am on Feb 25, 2012 (gmt 0)

I have tried all suggestions, added the L flag, put only 1 htaccess file in the root, but it just wont fly;
Basicly it boils down to this i suppose?

RewriteCond %{REQUEST_URI} ^/images/ [NC]
RewriteRule ^(.*)$ /images/$1 [NC,L]

RewriteCond %{HTTP_HOST} ^!www [NC]
RewriteRule ^(.*)$ /shops/index.php [NC,L]

No luck so then i change the www part to:

RewriteCond %{REQUEST_URI} !^/images/ [NC]
RewriteCond %{HTTP_HOST} ^!www [NC]
RewriteRule ^(.*)$ /shops/index.php [NC,L]

But still no luck still does:
computer.webzjop.info/images/ -> /shops/index.php

And then tried, since this had always worked for me on all other sites:
RewriteRule ^/images/(.*)$ /images/$1 [NC,L]

Still no luck?

I use the daddy host, you know what i mean i suppose. dont want to make an advertisement. Should that matter?

Should i now be trying to acchieve this with {REQUEST_URI} ?

g1smd




msg:4421619
 8:41 am on Feb 25, 2012 (gmt 0)

It may help to list out in plain English all the things you want to happen.

Redirect URL x to URL y

Rewrite URL y to folder path a

Rewrite URL z to folder path b

The requirements are not at all clear to me, and without clear requirements it is not possible to start coding.

ikbenhet4




msg:4421621
 9:11 am on Feb 25, 2012 (gmt 0)

Beasicly all i want is the following:

Redirect www.webzjop.info to file /index.php

Redirect subdomains.webzjop.info/ to file /shops/index.php

Redirect subdomains.webzjop.info/images/ to folder /images/

g1smd




msg:4421633
 10:19 am on Feb 25, 2012 (gmt 0)

Are you sure you want to externally redirect to a different URL?

Should some of those be internal rewrites to a different internal path?

ikbenhet4




msg:4421643
 10:42 am on Feb 25, 2012 (gmt 0)

Are you sure you want to externally redirect to a different URL?
These should all be internal rewrites, the url in the adress bar should not change.

I dont know if another example is goiing to be more clear but here goes:

www.webzjop.info - root directory of hosting package - path: /index.php

subdomains.webzjop.info - the /shops/ directory - path: /shops/index.php

subdomains.webzjop.info/images/ - the /images/ directory - path: /images/

goal:
-subdomains.webzjop.info/images/image.jpg should show file /images/image.jpg
-subdomains.webzjop.info/images/1/image2.jpg should show file /images/1/image2.jpg

g1smd




msg:4421670
 12:25 pm on Feb 25, 2012 (gmt 0)

Now you're talking the right language. A rewrite maps a URL request used "out there" on the web to an internal filepath "here" inside the server. (Conversely, an external redirect therefore redirects a URL request to another URL.)

The index file rewrite can be performed by the DirectoryIndex directive. You should also redirect incoming external requests for index.php and index.html URLs to a URL ending with a trailing slash.

lucy24




msg:4421680
 12:41 pm on Feb 25, 2012 (gmt 0)

Is there any point where all your domains and subdomains pass through the same htaccess file? That makes the difference between a simple Rewrite and an icky messy Rewrite involving a Proxy.

ikbenhet4




msg:4421682
 12:52 pm on Feb 25, 2012 (gmt 0)

The index file rewrite can be performed by the DirectoryIndex directive.
I dont get how directoryindex is gooing to save my ass. Its not like i am getting a 404 not found error. If you could elaborate please.

May i once more try to give the simplest example i can think of, of what i want:
RewriteRule ^(.*)$ /shops/index.php [NC]

Above rule but:
-Dont rewrite it if request directory starts with /images/
-Dont rewrite if the host starts with www

ikbenhet4




msg:4421684
 12:57 pm on Feb 25, 2012 (gmt 0)

Is there any point where all your domains and subdomains pass through the same htaccess file? That makes the difference between a simple Rewrite and an icky messy Rewrite involving a Proxy.
Yes.!.evertyhing goes through the same htaccess file. I long back to the old days where i had a dedicated server i could just in httpd.conf say:


<VirtualHost webzjop.info:80>
DocumentRoot /var/www/html
ServerName webzjop.info, www.webzjop.info
</VirtualHost>


<VirtualHost webzjop.info:80>
DocumentRoot /var/www/html/shops
ServerName webzjop.info
ServerAlias *.webzjop.info
</VirtualHost>

I actually created the script on my computer at home with these settings; thats why i am now struggeling with the shared hosting settings.

ikbenhet4




msg:4421691
 1:30 pm on Feb 25, 2012 (gmt 0)

sorry that i keep on posting; but is there an syntax error i am making on the following?
RewriteRule !^images/$ /shops/index.php [NC]

I think it should mean: if the directory is anything other than images do your rewrite, but it is not working.

g1smd




msg:4421704
 2:45 pm on Feb 25, 2012 (gmt 0)

You also need to add a RewriteCond for !^/shops/index\.php otherwise it will loop forever.

Don't forget the [L] flag. Use it on every RewriteRule. You don't need the [NC] flag.

Are you really sure that you want requests for robots.txt and so on to be rewritten to your script? It's better to test for what you DO want to rewrite and that is very easy if you use extensionless URLs for pages.

lucy24




msg:4421828
 1:17 am on Feb 26, 2012 (gmt 0)

sorry that i keep on posting; but is there an syntax error i am making on the following?
RewriteRule !^images/$ /shops/index.php [NC]

I think it should mean: if the directory is anything other than images do your rewrite, but it is not working.

It means: if the request is for anything other than the exact name
images/

So the only thing you are excluding is
www.example.com/images/index.html

or .php or whatever it is. If you want to exclude everything inside the top-level /images/ directory, you have to leave off the ending anchor.

There are situations where a leading ! is appropriate in a RewriteRule, but they are very rare and must be approached with extreme caution. Most of the time, it is safer to keep your ! in the Conditions.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved