【问题标题】:mod_rewrite code returns wrong domain, changed symbolic link to actualmod_rewrite 代码返回错误的域,将符号链接更改为实际
【发布时间】:2011-04-21 00:40:08
【问题描述】:

我有同一个网站的两个域。

我的网站上有一个页面,我希望它看起来来自第二个域(在本例中为 example2.com/pagex)。除了 root 和 pagex,所有其他对 example2.com 的请求都应重定向到 example1.com

由于下面显示的 mod_rewrite 规则,我网站上的所有页面都显示为目录,但实际上只是 index.php?page=pagename

这是我的 .htaccess 文件:

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{REQUEST_URI} !^/$ #Do not redirect root
RewriteCond %{REQUEST_URI} !^/pagex/ #Do not redirect pagex
RewriteRule ^(.*)$ http://www.example1.com/$1 [R=301]

# If the file or folder does not exist, send to index.php as variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]

通过转到 example2.com/pagex/ 运行上述代码,我会被重定向到 www.example1.com/index.php?page=pagex。第二个条件应该失败,阻止重定向。更奇怪的是虚拟目录被实际路径替换了。

【问题讨论】:

  • 那么,/pagex/ 是一个实际存在的目录吗?
  • /pagex/ 不是实际目录。 index.php?page=pagex 被屏蔽了。

标签: apache .htaccess mod-rewrite


【解决方案1】:

你可以这样写你的规则:

RewriteEngine on
Options +FollowSymlinks -MultiViews

RewriteCond %{HTTP_HOST} ^(www\.)?example2\.com [NC]
RewriteCond %{THE_REQUEST} !/pagex/? [NC]
RewriteRule ^(.+)$ http://www.example1.com/$1 [R=301,L]

# If the file or folder does not exist, send to index.php as variable
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ /index.php?page=$1 [L]

使用上面的代码http://example2.com/pagex/ 被正确重定向到http://example2.com/index.php?page=pagex

【讨论】:

  • 我试图让example2.com/pagex 根本不重定向,这是我要重定向的网站上的所有其他页面。我还测试了您提供的代码,仍然将其重定向到 example1.com/index.php?page=pagex(不是 example2)。
  • 如果是这种情况,那么在第二个重定向规则中也需要RewriteCond %{REQUEST_URI} !^/pagex/ [NC]。我刚刚更新了我的答案,请再试一次。
  • 我收到此回复:#1 Server Response: example2.com/pagex/ HTTP/1.1 301 Moved Permanently Date: Thu, 21 Apr 2011 04:13:18 GMT Server: Apache Location: http://www.example1.com/missing.html Vary: Accept-Encoding Content-Type: text/html; charset=iso-8859-1
  • missing.html 不在上述规则中。您是否有其他一些规则从某个地方触发,否则您可能在某个地方有ErrorDocument 404 http://example1.com/missing.html
  • 是的,添加了?(可选)以使其成为RewriteCond %{THE_REQUEST} !/pagex/? [NC] 请再试一次,它应该可以工作。
猜你喜欢
  • 2015-05-06
  • 2020-11-14
  • 1970-01-01
  • 2018-04-27
  • 2019-07-18
  • 2018-04-05
  • 2014-07-18
  • 2011-10-19
  • 1970-01-01
相关资源
最近更新 更多