mod_rewrite 代码返回错误的域,将符号链接更改为实际的
我的同一个网站有两个域。
我的网站上有一个页面,我希望显示为来自第二个域(在本例中为 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 >。第二个条件应该失败,从而阻止重定向。更奇怪的是虚拟目录已被替换为实际路径。
I have two domains for the same website.
I have a single page on my site I want to appear to come from the second domain (in this example it is example2.com/pagex). With exception to root and pagex, all other requests to example2.com should be redirected to example1.com.
All pages on my site appear as directories but are really just index.php?page=pagename due to mod_rewrite rules shown below.
Here is my .htaccess file:
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]
Running the above code by going to example2.com/pagex/ I get redirected to www.example1.com/index.php?page=pagex. The second condition should have failed, preventing the redirect. What is even stranger is the virtual directory has been replaced with the actual path.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以这样编写规则:
使用上面的代码
http://example2.com/pagex/
正确重定向到http://example2.com/index.php?page=pagex
You can write your rules like this:
With above code
http://example2.com/pagex/
gets correctly redirected tohttp://example2.com/index.php?page=pagex