Link to home
Start Free TrialLog in
Avatar of jeremyBass26
jeremyBass26Flag for United States of America

asked on

rewrite form any subdomain to media subdomain

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

Avatar of ravenpl
ravenpl
Flag of Poland image

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?
Avatar of jeremyBass26

ASKER

"www" stripped yes
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]
sorry for the delay, testing now... thank you...
that just hands off a 500 error... any ideas?

Thank you Cheers
Jeremy
Anything strange in the urlbar?
Try adding

Options +FollowSymLinks
RewriteEngine On

before rewrites
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

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]
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?
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?
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
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]
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


> 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)?
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

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]
I meant on "I need to still force the www if none at all.." on every page... like the html ect...

gosh :D
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)
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?

ASKER CERTIFIED SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
sorry for the delay