mod_rewrite 代码返回错误的域,将符号链接更改为实际的

发布于 2024-11-02 07:57:07 字数 934 浏览 0 评论 0原文

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

我的网站上有一个页面,我希望显示为来自第二个域(在本例中为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

书信已泛黄 2024-11-09 07:57:07

您可以这样编写规则:

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

You can write your rules like this:

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]

With above code http://example2.com/pagex/ gets correctly redirected to http://example2.com/index.php?page=pagex

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文