linux - 如果找不到文件或文件夹,.htaccess 重定向到新域

标签 linux .htaccess

如果我的网站是domain.com,并且如果我转到domain.com/1并且文件夹1不存在,我可以通过.htacces重定向到domain2.com/1

如果文件或文件夹存在,我想显示该文件或文件夹,但如果不存在,则重定向到新域

示例:如果 file.html 在我的服务器中不存在

example.com/file.html 重定向到 newdomain.com/file.html

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ newdomain.com/$1     

最佳答案

您需要包含 http:// 位:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R]

否则,mod_rewrite 会将其解释为 URL 或文件路径,并尝试查找名为“newdomain.com”的目录。如果您希望重定向永久有效,请将 R 标志更改为 R=301

关于linux - 如果找不到文件或文件夹,.htaccess 重定向到新域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19058127/

相关文章:

apache - 特定 RewriteCond 的自定义 403 页面

c++ - 无法分析库中的线程

c - 是什么导致此链接失败( undefined reference )?

c++ - GDB加载so文件失败并报错No such file or directory

image - 如何使用 apache 或 .htaccess 限制对某种内容的访问?

php - 在 justhost 帐户上使用 cakephp 的 htaccess 500 错误

apache - 如何为除 localhost 之外的所有域提供动态虚拟主机

.htaccess - htaccess - 禁用除 www 之外的子域

linux - 如何解析shell脚本中的以下内容?

linux - 在Linux中设置目录