.htaccess Apache modrewrite htaccess重定向

.htaccess Apache modrewrite htaccess重定向,.htaccess,mod-rewrite,.htaccess,Mod Rewrite,我已经尝试了我能找到的每一种变体(在堆栈溢出和其他地方找到了示例,但它们也不适合我的情况) 这是我修改过的.htaccess文件: DirectoryIndex index.html RewriteEngine On # New Try - apparently does nothing RewriteCond %{SERVER_PORT} 80 RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com RewriteCond %{REQUEST_URI} ^\

我已经尝试了我能找到的每一种变体(在堆栈溢出和其他地方找到了示例,但它们也不适合我的情况)

这是我修改过的.htaccess文件:

DirectoryIndex index.html
RewriteEngine On

# New Try - apparently does nothing
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com
RewriteCond %{REQUEST_URI} ^\/?snafu\/?(.*)$
RewriteRule ^(.*) https://snafu.com/%1 [R,L]

# First try - apparently does nothing
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com
RewriteRule ^\/?snafu\/?(.*)$ https://snafu.com/$1 [R,L]

# this works as expected
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?fubar\.com
RewriteRule ^(.*)$ https://www.fubar.com/$1 [R,L]
我在上面展示了我试图引起的两种方式 重定向到

这两段都没有任何作用

文件中的最后一段按预期工作,因此我知道Apache 就是把事情做好

显然,该文件在某些情况下有效,但我无法找出其他情况下的错误

根据建议,我将上述所有代码替换为:

DirectoryIndex index.html
Redirect 301 /snafu https://snafu.com
当我试图浏览到

我收到Firefox消息:页面没有正确重定向 Firefox检测到服务器正在以一种永远无法完成的方式重定向对此地址的请求

但是,浏览器URL窗口将显示所需的目标

Chrome报告:snafu.com页面不工作 snafu.com重定向您的次数太多。 错误\u太多\u重定向

同样,浏览器URL窗口显示所需的目的地

Internet Explorer说:找不到 在此服务器上找不到请求的URL/snafu.com/index.html

Opera说:snafu.com页面不起作用,snafu.com重定向了你太多次

Safari说:Safari无法打开页面“”,因为Safari无法与服务器“snafu.com”建立安全连接

我不知道这是否重要,但fubar.com和snafu.com都有相同的IP地址-如果我清除浏览器缓存,我可以直接导航到,没有问题,因此再次显示Apache正在正常工作

snafu.com根目录包含下面的.htaccess文件,该文件似乎按预期工作:

DirectoryIndex index.html
RewriteEngine On 

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?snafu.fubar\.com
RewriteRule ^(.*)$ https://snafu.com/$1 [R,L]

RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?snafu\.com
RewriteRule ^(.*)$ https://snafu.com/$1 [R,L]
删除此文件不会更改任何浏览器行为

我的初步结论是,简单的重定向301/snafu命令很混乱,因为snafu和fubar具有相同的IP地址。但是ModRewrite应该更聪明——我只是不够聪明,不能想出正确的咒语


我哪里做错了?我的目标是将所有引用fubar.com/snafu的现有链接重定向到snafu.com,这样搜索引擎就不会被访问同一内容的两种不同方式所混淆。

您能尝试将其添加到您的htaccess中吗

Redirect 301 /snafu http://snafu.com