[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

rewrite form any subdomain to media subdomain

Posted on 2009-12-17
21
Medium Priority
?
1,066 Views
Last Modified: 2012-05-08
Hello, I want to have only .jpgs and what not to come out of the subdomain "media" regardless of where the request cam e from.. www. or no subdomain defined at all.. Here is what I was trying.. Not seeming to work... Thank you for the help...

Cheers
Jeremy


<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$">
# force CMSMS media Subdomain IN URL
RewriteCond %{HTTP_HOST} ([^.]+)\.tgminvestmentsllc.com[NC]
RewriteRule ^(.*)$ http://media.%{HTTP_HOST}/$1 [R=301,L]
# END CMSMS media Subdomain IN URL
</FilesMatch>
<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$">
# force CMSMS media Subdomain IN URL 
RewriteCond %{HTTP_HOST} ([^.]+)\.tgminvestmentsllc.com[NC] 
RewriteRule ^(.*)$ http://media.%{HTTP_HOST}/$1 [R=301,L] 
# END CMSMS media Subdomain IN URL 
</FilesMatch>

Open in new window

0
Comment
Question by:jeremyBass26
  • 12
  • 9
21 Comments
 
LVL 43

Expert Comment

by:ravenpl
ID: 26073926
I believe FilesMatch will not affect the rewrites in any way

I also don't understand Your needs precisely, please explain

#if request to one of those files
RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
#and not media.*
RewriteCond %{HTTP_HOST} ! ^media\. [NC]
#redirect to media.*
RewriteRule .* http://media.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

but do You want media.www.domain.com, or You want the "www" stripped? Maybe You should redirect to media.%{SERVER_NAME} but %{SERVER_NAME} stripped from leading part?
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26074182
"www" stripped yes
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26074365
RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} ! ^media\. [NC]
RewriteCond %{HTTP_HOST} ! ^(www\.)?(.*) [NC]
RewriteRule .* http://media.%2%{REQUEST_URI} [R=301,L]
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 4

Author Comment

by:jeremyBass26
ID: 26097604
sorry for the delay, testing now... thank you...
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26097662
that just hands off a 500 error... any ideas?

Thank you Cheers
Jeremy
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26097754
Anything strange in the urlbar?
Try adding

Options +FollowSymLinks
RewriteEngine On

before rewrites
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26097979
no it's a internal server error... here is what I have.. the full .htaccess file...
# BEGIN Optional settings

# Turns off directory browsing
# not absolutely essential, but keeps people from snooping around without 
# needing empty index.html files everywhere
Options -Indexes
Options +FollowSymLinks
# Deny access to config.php
# This can be useful if php ever breaks or dies
# Use with caution, this may break other functions of CMSms that use a config.php
# file.  This may also break other programs you have running under your CMSms
# install that use config.php.  You may need to add another .htaccess file to those
# directories to specifically allow config.php.
<Files "config.php">
order allow,deny
deny from all
</Files>

# Sets your 403 error document
# not absolutely essential to have, 
# or you may already have error pages defined elsewhere
ErrorDocument 403 /forbidden403.shtml

# No sense advertising what we are running
ServerSignature Off

# END Optional Settings

# BEGIN CMSMS and Rewrite Rules
# Make sure you have Options FollowSymLinks
# and Allow on

RewriteEngine On

# Might be needed in a subdirectory
#RewriteBase /
# URL Filtering helps stop some hack attempts
#IF the URI contains a "http:"
RewriteCond %{QUERY_STRING} http\: [OR]

#OR if the URI contains a "["
RewriteCond %{QUERY_STRING} \[ [OR]

#OR if the URI contains a "]"
RewriteCond %{QUERY_STRING} \] [OR]

#OR if the URI contains a "<script>"
RewriteCond %{QUERY_STRING} (\<|%3C).*script.*(\>|%3E) [NC,OR]

#OR script trying to set a PHP GLOBALS variable via URL
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]

#OR any script trying to modify a _REQUEST variable via URL
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2}) [OR]

#IF the URI contains UNION
RewriteCond %{QUERY_STRING} UNION [OR]

#OR if the URI contains a *
RewriteCond %{QUERY_STRING} \*

#then deny the request (403)
RewriteRule ^.*$ - [F,L]

# End URL Filtering

# force CMSMS www IN URL 
#RewriteCond %{HTTP_HOST} !^www\. [NC] 
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L] 
# END CMSMS force www IN URL 

RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} ! ^media\. [NC]
RewriteCond %{HTTP_HOST} ! ^(www\.)?(.*) [NC]
RewriteRule .* http://media.%2%{REQUEST_URI} [R=301,L]



# CMSMS Rewriting
# Set assume mod_rewrite to true in config.php and clear CMSMS cache
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.+)$ index.php?page=$1 [QSA]




# CMSMS Header Expires
# 3months
#<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav)$">
#  Header set Cache-Control "max-age=7257600"
#  Header set Expires "Thu, 15 Apr 2010 20:00:00 GMT"
#  Header unset Last-Modified
#</FilesMatch>
# WEEK
#<FilesMatch "\.(js|css|swf|gz)$">
#  Header set Cache-Control "max-age=604800"
#</FilesMatch>
# 45 MIN
#<FilesMatch "\.(html|htm|txt)$">
#  Header set Cache-Control "max-age=2700"
#</FilesMatch>

<FilesMatch "\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css)$">
Header unset Last-Modified
Header set Expires "Fri, 21 Dec 2012 00:00:00 GMT"
Header set Cache-Control "public, no-transform"
Header set Cache-Control "max-age=7257600"
</FilesMatch>

# CMSMS Safari gzip fix
# AddEncoding gzip .gz
# RewriteCond %{HTTP:Accept-encoding} gzip
# RewriteCond %{HTTP_USER_AGENT} !Safari|!playstation\ 3 [NC]
# RewriteCond %{REQUEST_FILENAME}.gz -f
# RewriteRule ^(.*)$ $1.gz [QSA,L]



# CMSMS unset ETag
Header unset ETag
FileETag None
#<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|gz|swf|mp3|wav)$">
#Header unset ETag
#FileETag None
#</FilesMatch>

# CMSMS block unwanted HTTP_REFERER
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !^https://(www\.)?digitalbarn.tv/.*$ [NC]
# RewriteRule \.(ico|pdf|flv|jpg|jpeg|png|gif|swf)$ /feed/ [R=302,L]



# END CMSMS

Open in new window

0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26098560
Can You try changing
RewriteCond %{HTTP_HOST} ! ^(www\.)?(.*) [NC]
RewriteRule .* http://media.%2%{REQUEST_URI} [R=301,L]

to
RewriteCond %{HTTP_HOST} ! ^w{0,3}\.?(.+) [NC]
RewriteRule .* http://media.%1%{REQUEST_URI} [R=301,L]
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26099049
no luck I change it to

RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} ! ^media\. [NC]
RewriteCond %{HTTP_HOST} ! ^w{0,3}\.?(.+) [NC]
RewriteRule .* http://media.%1%{REQUEST_URI} [R=301,L]

but still a 500 error..

not sure .. is it looping?
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26099272
There's a bug, but it not responsible for error 500.
< RewriteCond %{HTTP_HOST} ! ^w{0,3}\.?(.+) [NC]
should not be negated
> RewriteCond %{HTTP_HOST} ^w{0,3}\.?(.+) [NC]

I've no idea bout the 500 cause. Can You consult apache's error log?
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26106224
ok the log is...
[Mon Dec 21 11:40:56 2009] [alert] [client 69.92.188.32] /var/www/vhosts/*.com/httpdocs/.htaccess: RewriteCond: bad flag delimiters

"*" is a replacement by me
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26107660
Try sticking the ! to pattern (can that be so simple?)

RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} !^media\. [NC]
RewriteCond %{HTTP_HOST} ^w{0,3}\.?(.+) [NC]
RewriteRule .* http://media.%1%{REQUEST_URI} [R=301,L]
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26108004
all most.. if there is www. then it works.. it needs to work for all subdomains...

so if it was domain.com or script.domain.com

It's close I feel it now :D


0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26108050
> so if it was domain.com or script.domain.com
domain.com -> media.domain.com
www.domain.com -> media.domain.com
script.domain.com -> media.domain.com
anything.domain.com -> media.domain.com
Right?
Can I assume that the domain is always TLD(has one dot only)?
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26108123
yes if in that list of targets then convert from

domain.com -> media.domain.com
www.domain.com -> media.domain.com
script.domain.com -> media.domain.com
anything.domain.com -> media.domain.com

on .. "Can I assume that the domain is always TLD(has one dot only)?"

um.. not sure I fallow you there.. you mean

domain.com.en ?

or
any.thing.domain.com

yes?
:D

0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26108154
shoot this throu it off..

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

I need to still force the www if none at all.. dang... I only had it commented to work on this.. no error but .. hmm what you think..

this is what was above in the main file... sorry about that

# force CMSMS www IN URL
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# END CMSMS force www IN URL

RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} !^media\. [NC]
RewriteCond %{HTTP_HOST} ^w{0,3}\.?(.+) [NC]
RewriteRule .* http://media.%1%{REQUEST_URI} [R=301,L]
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26108160
I meant on "I need to still force the www if none at all.." on every page... like the html ect...

gosh :D
0
 
LVL 43

Expert Comment

by:ravenpl
ID: 26108362
I was asking, if the domain.com is always the same, or same length, or same dots number? I have to match the domain itself only.

Also, You either cannot "# force CMSMS www IN URL " or we have to use if-then-else construction(to avoid infinite loop)
0
 
LVL 4

Author Comment

by:jeremyBass26
ID: 26108442
yes domain.com  will be the same for what it's handleing.. but this will be use on 3 sites all with different lenghts...


So if this
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

would cause a infinite loop and I think I see why (after a rewrite it runs thru the rules again?)

could it be said, if no subdomain force www
if in the target write all subdomains to media?

that I'd think wouldn't cause a loop right?

0
 
LVL 43

Accepted Solution

by:
ravenpl earned 2000 total points
ID: 26117725
OK, if it's the same domain, then try

RewriteCond %{REQUEST_URI} !\.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

RewriteCond %{REQUEST_URI} \.(ico|gif|jpg|jpeg|png|flv|pdf|mp3|wav|js|css|kml|xml)$
RewriteCond %{HTTP_HOST} !^media\.domain\.com [NC]
RewriteRule .* http://media.domain.com%{REQUEST_URI} [R=301,L]

0
 
LVL 4

Author Closing Comment

by:jeremyBass26
ID: 31667399
sorry for the delay
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question