.htaccess selective redirecting

Sharky

Community Paragon
Community Support
Messages
4,399
Reaction score
94
Points
48
So... I'm trying to do something with .htaccess, and it won't work...

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx
RewriteCond %{REQUEST_URI} $.jpg|.jpeg|.png|.gif
RewriteRule ^(.*)$ http://sharkyonline.co.cc.nyud.net/-/$1 [R,L]
</IfModule>

I want it to check the user agent, if it's not CoralWebPrx, I want it to check the filetype, if it's jpg, jpeg, png, or gif, I want it to redirect it to a new URL on a different server. Now, it works without the REQUEST_URI line. WITH the REQUEST_URI line, it makes no difference if the rewrite block is there or not.

Any ideas?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Not that familiar with mod_rewrite regex syntax, but

Code:
RewriteCond %{REQUEST_URI} (.jpg|.jpeg|.png|.gif)$

might work
 

Sharky

Community Paragon
Community Support
Messages
4,399
Reaction score
94
Points
48
You, sir, are a legend. Thankyou.

Any idea how to write an exclusion to make sure that a URL doesn't contain a certain character or string? eg., I have all my addon domains in public_html/_/<addon domain dir> . In short, I'd like to have a .htaccess in the /public_html directory that doesn't screw up all the addon domains.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
You, sir, are a legend. Thankyou.

Any idea how to write an exclusion to make sure that a URL doesn't contain a certain character or string? eg., I have all my addon domains in public_html/_/<addon domain dir> . In short, I'd like to have a .htaccess in the /public_html directory that doesn't screw up all the addon domains.

You mean a additional rule to the original two so that image files for the addon domains do not get redirected along with those for the main domain?
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
1. I am not 100% sure how they handle addon domains. From what I gather if you add sharkysworld.com to your account, the root directory for that domain is
/public_html/_/sharkeysworld.com/
Then I assume they use mod_rewrite to adjust the incoming urls.

With those assumptions....

RewriteCond %{HTTP_HOST} !(sharkeysworld\.com|sharkbait\.org|sharktank\.net) [NC]
RewriteCond %{REQUEST_URI} !(sharkeysworld\.com|sharkbait\.org|sharktank\.net)

might do the trick. If the .htaccess contains rewrites that already map the addon domains to subdirectories, all you would need is the second line.
 

Sharky

Community Paragon
Community Support
Messages
4,399
Reaction score
94
Points
48
Fantastic. It works!

For anyone finding this via the searching, and who wants to use the CoralCDN to load images from their website (without interfering with addon domains), you can use this in your .htaccess file:

Code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !(domain2.com|domain3.com) [NC]
RewriteCond %{REQUEST_URI} !(domain2.com|domain3.com) [NC]
RewriteCond %{HTTP_USER_AGENT} !^CoralWebPrx [NC]
RewriteCond %{REQUEST_URI} (.jpg|.jpeg|.png|.gif)$ [NC]
RewriteRule ^(.*)$ http://domain1.com.nyud.net/$1 [R,L]
</IfModule>


Without the exclusions, addon domains that are located in a subdirectory of the main domain will be affected by the .htaccess. Eg., having your main domain as domain1.com and an addon domain of domain2.com , if you store the code in the .htaccess for domain1.com , visiting domain2.com will load images from domain1.com.nyud.net/sitesdir/domain2.com/file.jpeg .

Credit to descalzo for correcting my attempts at filtering by file extension, and for coming up with the method for excluding addon domains.

Thanks again!
 
Last edited:
Top