Forum Moderators: phranque

Message Too Old, No Replies

Rewrite rule for subdomain

         

sam222

8:45 pm on Oct 5, 2014 (gmt 0)

10+ Year Member



I have my rule which works fine:

RewriteRule ^gallery/(default|large|thumbnail)/([^/]+\.(gif|png|jpg|jpeg))$ /CRM/DIR/$1/$2 [L]

It works fine when i use http://www.example.com/...

I want to rewrite this rule to be applied on my subdomain so that i would be able to: http://subdomain.example.com/...

lucy24

2:31 am on Oct 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Not enough information. Right now I don't see anything in the rule that would prevent it from working on any and all hosts.

Do you mean that you want to serve the same content regardless of what the specified hostname is? Ordinarily we'd be screaming Nooooo! but I guess it's not as severe an issue if you're only concerned with image files. Maybe an external redirect for search engines-- not to the files' "real" location, just to their primary-domain URL-- so they don't ask for (and index) multiple copies of the same picture.

A subdomain by its nature always involves some rewriting, so what we need to do is point the rewrites in different directions depending on whether the request is for a page or for an image. How are your subdomains currently set up? That is, what's the physical directory structure and what do the basic rewrites look like?

Psst! "jpg|jpeg" can also and more efficiently be expressed as "jpe?g".

Edit: I'm assuming the subdomains live on the same server and the same userspace-or-equivalent as the primary. If they live on different servers, we're looking at proxies. Ugh.

sam222

7:49 am on Oct 6, 2014 (gmt 0)

10+ Year Member



I am sorry i couldn't explain well.

Is it possible to rewrite a sub domain to a directory where sub domain exists under the same server.

Example: http://sub-domain.example.com/directory/myfile.html shows the content of http://example.com/directory/subdirectory/myfile.html

[edited by: Ocean10000 at 2:23 pm (utc) on Oct 6, 2014]
[edit reason] examplified [/edit]

sam222

9:43 am on Oct 6, 2014 (gmt 0)

10+ Year Member



I tried to search and found a solution which didn't work in my case:

.htaccess is in the root and trying to do below:

RewriteCond %{HTTP_HOST} ^subdomain.example.com/gallery/images/(default|large|thumbnail)/([^/]+\.(gif|png|jpg|jpeg))

RewriteRule ^(.*)$ /subdirectory1/subdirectory2/$1/$2 [L,NC,QSA]


both subdirectory1 and subdirectory2 exist on main domain http://example.com/subdirectory1/subdirectory2

I can't use absolute path with my main domain like http://example.com/subdirectory1/subdirectory2

lucy24

3:42 pm on Oct 6, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Never mind about the subdirectories in the URL. You still need to explain how the subdomain currently works-- assuming you've already got some in place. Since

subdomain.example.com/

has no physical meaning, there has to be some rewriting, most likely (but not necessarily) to

example.com/subdomain/

RewriteCond %{HTTP_HOST} ^subdomain.example.com/gallery/images/(default|large|thumbnail)/([^/]+\.(gif|png|jpg|jpeg))

That's not the host. The %{HTTP_HOST} is only the part before the first directory slash, here
subdomain.example.com

sam222

4:50 pm on Oct 6, 2014 (gmt 0)

10+ Year Member



I do appologize again not to be very clear. My images exist in the root folders as public_html/t1/t2/

To access images my absolute URL path is: http://www.example.com/t1/t2/default/1.jpg
I want to rewrite this URL so that i would be able to access through:
http://feed.example.com/gallery/images/default/1.jpg

For this I have added the rewrite code in my .htaccess file and moved this file in 'feed' subdomain

RewriteRule ^gallery/images/(default|large|thumbnail)/([^/]+\.(gif|png|jpg|jpeg))$ /public_html/t1/t2/$1/$2 [L]

It is giving 404 error because unable to access the path: /public_html/t1/t2/ because this path exists on root and not inside 'feed' subdomain. I can't use my www domain in the place of /public_html/t1/t2/

lucy24

7:39 am on Oct 7, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I've got a nasty feeling you may need a proxy after all.

:: wandering off in search of g1smd's email address because I'm suddenly getting a severe attack of jamais vu w/r/t subdomains ::

phranque

12:52 pm on Oct 13, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



For this I have added the rewrite code in my .htaccess file and moved this file in 'feed' subdomain

RewriteRule ^gallery/images/(default|large|thumbnail)/([^/]+\.(gif|png|jpg|jpeg))$ /public_html/t1/t2/$1/$2 [L]

It is giving 404 error because unable to access the path: /public_html/t1/t2/ because this path exists on root and not inside 'feed' subdomain. I can't use my www domain in the place of /public_html/t1/t2/


http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
in the case of using rewrites in a .htaccess file, relative to your document root


to do what you want you must have galleries in the same document root directory as t1.

it's also possible to do something with symbolic links but apache disables following symbolic links by default and using these opens security holes.

lucy24

5:21 pm on Oct 13, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



apache disables following symbolic links by default

Senior moment, phranque? :)
To enable the rewrite engine in this [.htaccess or Directory] context ... "Options FollowSymLinks" must be enabled.

If Symbolic Links weren't enabled, the htaccess file wouldn't work at all.

By weird coincidence, only a few days ago I was asking someone how subdomains worked physically (we are on the same host). Her answer included explicit reference to symbolic links.