.htaccess - 在 RedirectRule 发生后重定向到其他网站
问题描述
我有一个网站,比方说www.example.com
,但这曾经是www.example.nl
. 来自的所有流量www.example.nl
现在都重定向到www.example.com
,但是由于我们更改了一些命名约定,www.example.nl/seeds
现在必须重定向到www.example.com/nl/flower-seeds
。
我得到的.htaccess
文件包含以下代码:
RewriteEngine On
RewriteBase
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
当我导航到时,www.example.nl/seeds/
我最终www.example.com/seeds/
以 404 结尾,因为我错过了该/nl/flower-
部分。
我认为当 URL 已被 RewriteRule 更改时,重定向无法正常工作。你将如何解决这个问题?任何帮助表示赞赏!
解决方案
您应该排除 /seeds/
这样的目录形式一般规则:
RewriteEngine On
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteCond %{REQUEST_URI} !/seeds/(.*)
RewriteRule ^(.*)$ http://www.example.com/$1 [L, R=301]
RewriteCond %{HTTP_HOST} !example.com$ [NC]
RewriteRule ^seeds/(.*)$ http://www.example.com/nl/flower-seeds/$1 [L, R=301]
注意:清除浏览器缓存然后测试
另外,不要像您所做的那样使用带有重定向的正则表达式:
Redirect 301 /seeds/(.*) example.com/nl/flower-seeds/$1
在这里这(.*)
没有任何意义,你可以使用任何一个RedirectMatch or RewriteRule
推荐阅读
- spring - Spring Boot JPA Repository:在 Spring Boot 中从普通 Java 类访问 JPA 存储库函数
- python - uWSGI + Django:ImportError:没有名为“wsgi”的模块
- winapi - 在启用 GDI 缩放的 Windows 10 上保存位图
- php - 使用 PHP 修改 WordPress 中的类别模板
- angular - Angular 6 lib 提供程序错误
- bootstrap-4 - 如何限制 Bootstrap datetimepicker 未来的日期?
- machine-learning - 使用 Fortran 导入 MNIST 数据集
- typo3 - TYPO3 - 翻译不显示,总是默认语言
- javascript - jquery加载函数不调用spring boot控制器来替换thymeleaf模板片段
- angular - 如何像我们在angular js中但在angular 5中一样包含html文件?