简体   繁体   中英

.htaccess redirect to external url but with mod_rewrite

I have two webspaces, one domain. One webspace contains ~ 5 TB Audiofiles & Images, and one webspace is for the script.

My problem:

Take for eg my webspace url are domain1.tld & domain2.tld

On domain2.tld are all(!) files and and other things. On domain1.tld i uploaded only the php files.

The problem is that if i want to see a video, which is for eg on domain2.tld/files/video01.mp4 and the link shows domain1.tld/files/video01.mp4 - how can i change this if this file not exist to go to domain?

I saw here a similar question.

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?mydomain\.com$ [NC]
RewriteRule ^subdirectory/?$ http://newurl.com [L,R=301,NC]

But this have 1 problem:

  1. If i go to http://domain1.tld/%FILE% and if this not exist -> it should be located to http://domain2.tld/%FILE%

And is it possible to work with mod_rewrite?

So i mean, if domain1.tld/%FILE% dont exist, it should be the same url but it should be the access of domain2.tld/%FILE%

Thanks!

Try that:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ http://domain2.tld/$1 [L,R=302,NC]

It means: if file or directory or symlink not found here, do redirect to domain2.tld .

(It's 302 because testing new rewrite rules with permanent redirect is dangerous )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM