2

I need to do some apache rewrite/redirect rules to external webservice in case of 404 error for specific file extensions: .jpg, .png, etc. Wordpress is used here.

So, if 404 occurs at:

https://test.com/folder/subfolder/year/month/filename.jpg

I want to redirect it to:

https://test1.com/folder/subfolder/year/month/filename.jpg (external webservice, not the same phisical server)

I've tried such a configuration in htaccess, didn't work as expected:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) test1.com/folder/subfolder//$year$\/$month$\/([^\s]+(\.(?i)(png | jpg | gif | svg))$)/

Do you have any ideas how to do it right way?

Any suggestions appreciated.

gaspar
  • 674
  • 2
  • 10
  • 24
  • Can these requests be located anywhere? Or is it a specific file base path as in your example, `/folder/subfolder/...`? Or a specific path format like `/////`? – MrWhite Apr 28 '22 at 11:13
  • Yes, exaclty. These are specific file paths. – gaspar Apr 28 '22 at 11:32
  • 1
    Are you using Wordpress? If yes then 404 is handled inside your theme's WP code. You will have to redirect using php code there not in .htaccess – anubhava Apr 29 '22 at 05:44
  • Yes @anubhava, wordpress is used here by developers. Can you please let me know how this rule should look like in php? – gaspar Apr 29 '22 at 06:28
  • 1
    As I said that would be php code not a rule. There will be many examples on WP forums for that – anubhava Apr 29 '22 at 06:30
  • 1
    Ok, I see. Many thanks to you for guidence! – gaspar Apr 29 '22 at 06:31
  • 1
    These are non-existent "image" files that are presumably served outside of WordPress, you don't _need_ to perform this redirect in PHP. – MrWhite Apr 29 '22 at 11:08

2 Answers2

0

With your shown samples, attempts; please try following htaccess rules file. These rules are written as per shown domain names which are samples/tests, so you need to change values as per your actual values when you use them in your system. We also need to make sure that both (test.com and test1.com) are sharing same directory structure in your actual apache server.

Also make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?test\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ https://test1.com/$1 [R=301,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Many thanks for quick response! test.com and test1.com are not hosted on the same phisical server. test1.com is an external webservice. – gaspar Apr 28 '22 at 12:00
  • @gaspar, oh ok not sure on it honestly. When you use these rules are you getting any error? kindly do let me know on same. – RavinderSingh13 Apr 28 '22 at 14:12
0

To "redirect" URLs of the form /folder/subfolder/<year>/<month>/<file>.<png|jpg|gif|svg> where /folder/subfolder/ is static and the other elements are variable and which do not exist on the filesystem you would need to do something like the following before the WordPress code block. ie. before the # BEGIN WordPress section.

# Redirect certain non-existent image files to another server
RewriteRule ^folder/subfolder/\d{4}/\d\d/[\w-]\.(png|jpg|gif|svg)$ https://test1.com/$0 [R=302,L]

# BEGIN WordPress
:

The <year> is a 4-digit number and <month> is a 2-digit number. The filename can consist of the characters 0-9, a-z, A-Z, _ (underscore) and - (hyphen).

This should presumably be a 302 (temporary) redirect, not a 301 (permanent), otherwise if the resource should become available at the source domain then it won't be accessible to those users who have visited the URL before (they will be redirected from cache).

To avoid the external redirect it may be preferable to "proxy" the request to the other domain. (This is invisible to the end user.) Although this potentially involves additional configuration server-side, as you would need to configure the source server as a "reverse proxy". You can then replace the R=302 flag in the above rule with P (proxy).

MrWhite
  • 43,179
  • 8
  • 60
  • 84