Monday, May 15, 2017

.htaccess file not redirecting assets to other domain

Leave a Comment

I'm trying to make my PHP site pull images in /assets/u/ from another server using a redirect in the .htaccess file. The .htaccess is definitely being used. I know because I set 'AllowOverride All' in the sites root directory in HTTPD config and even tested it by putting in incorrect syntax on purpose and confirming that I get an internal server error. But my images in /assets/u/ still aren't being pulled from the other server. Below is my .htaccess file...

SetEnv APPLICATION_ENV production  RewriteEngine On RewriteBase /  RewriteCond %{HTTP_HOST} !^((www)|(draft))\. RewriteRule ^(.*?)\/*$ http://www.%{HTTP_HOST}/$1 [R=301,L]  # Start of attempted redirect # RewriteCond %{REQUEST_URI} ^.*(/assets/u/).*$ [NC] RewriteRule ^assets/(.*)$ http://assets.otherserver.com/$1 [L,R=301] # End of attempted redirect #  RewriteCond %{REQUEST_FILENAME} -s [OR] RewriteCond %{REQUEST_FILENAME} -l [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^.*$ - [NC,L] RewriteRule ^.*$ index.php [NC,L] 

2 Answers

Answers 1

I don't fully understand your question but here is my 1st attempt.

I am assuming you are using assets/u/ as link in <img href="assets/u/test.jpg"> tag, and your actual files are stored in http://assets.otherserver.com address,

For this task I suggest you to use mod_proxy to display your images without redirecting the urls try with below rule I am assuming you are able to arrange your .htaccess without breaking the other rules.

RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} ^/assets/u/(.+?)$ RewriteRule ^ http://assets.otherserver.com/%1 [P,L] 

For above rule to work you must have enabled below.

LoadModule proxy_http_module modules/mod_proxy_http.so LoadModule proxy_module modules/mod_proxy.so 

Uncomment above lines in httpd.conf.

Inform me of any update.

Answers 2

As you use RewriteBase Directive, try to remove the the first /

RewriteCond %{REQUEST_URI} ^(assets/u/).*$ [NC] 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment