【问题标题】:htaccess redirect from subdir to root passing referrer as parameterhtaccess 从 subdir 重定向到 root,将引用者作为参数传递
【发布时间】:2017-09-20 18:13:58
【问题描述】:

我是 mod rewrite 的新手,我在尝试为我们的私人论坛用户实现以下目标时遇到了问题。私人论坛位于站点的子目录中,名为community

用户点击电子邮件链接:

https://www.example.com/community/index.php?/topic/this-is-the-topic

当浏览器进入论坛并且用户未登录时。论坛将/login/注入{QUERY_STRING},尽管它没有显示在URL中并且它们被定向到网站登录页面,这是唯一的方法根据所需的 RewriteRule 判断用户是否已登录。

到达登录页面时,URL 需要如下所示:

https://www.example.com/login.php?redirect_to=https://www.example.com/community/index.php?/topic/this-is-the-topic

我尝试过的是:

RewriteCond %{QUERY_STRING} /login/(.*)$ [NC]
RewriteRule ^(.*)$ https://www.example.com/login.php?redirect_to=%{REQUEST_URI} [L,R=301]

这让我明白了:

https://www.example.com/login.php?redirect_to=/community/index.php

问号后面的所有东西都不见了……

如果我在 RewriteRule 中的 {REQUEST_URI} 之后添加 %{QUERY_STRING},您可以看到 login 已在 {QUERY_STRING} 中的 /community 之后注入,然后是包含 Base64 编码版本的 &ref=电子邮件中的原始引用 URL。

更新:

社区是社区子目录中的私人论坛 并且有 2 个 .htaccess 文件,一个在根目录中,另一个在社区目录中。

我继承了这个,对我来说 htaccess 文件看起来有点乱 我们需要确保所有 http 流量都被强制到 https

根 .htaccess 文件

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTPS_HOST} !^tribe.mytechnologybusiness.com$ [NC]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (.*) https://tribe.mytechnologybusiness.com/$1 [L,R=301]
# End Replacement

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

# BEGIN MainWP
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^wp-content/plugins/mainwp-child/(.*)$ /wp-content/plugins/THIS_PLUGIN_DOES_NOT_EXIST [QSA,L]
</IfModule>
# END MainWP

社区 .htaccess 文件

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

############################################
# WordPress to IPBoard Integration
# Start Board to Wordpress Redirect Area
############################################
RewriteEngine on

# Registration
RewriteCond %{QUERY_STRING} /register/(.*)$ [NC]
RewriteRule (.*) https://tribe.mytechnologybusiness.com/wp-login.php?action=register [L,R=301]
# Logout
RewriteCond %{QUERY_STRING} /logout/(.*)$ [NC]
RewriteRule (.*) https://tribe.mytechnologybusiness.com/wp-login.php?action=logout [L,NE,R=301]
# Login
RewriteCond %{QUERY_STRING} /login/(.*)$ [NC]
RewriteRule ^(.*)$ https://tribe.mytechnologybusiness.com/wp-login.php?redirect_to=%{REQUEST_URI} [L,R=301]

############################################
# End Board to Wordpress Redirect Area
############################################

我试图用来测试的网址是

https://tribe.mytechnologybusiness.com/community/index.php?/topic/162-sydney-tribal-meetup-thursday-14th-september-2017/

谁能帮我解决这个问题?

干杯,

【问题讨论】:

    标签: .htaccess mod-rewrite


    【解决方案1】:

    您可以将此规则用作/community/.htaccess 中的第一条规则

    RewriteEngine On
    
    RewriteCond %{QUERY_STRING} /login/ [NC]
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(community/index\.php\S*) [NC]
    RewriteRule ^ /wp-login.php?redirect_to=https://%{HTTP_HOST}/%1 [L,NC,R=301,NE]
    
    # rest of your rules go here
    

    【讨论】:

    • 谢谢。它对我不起作用......我在文件中有其他规则。一些来自 WordPress 和其他强制 http 到 https 重定向。社区目录也有自己的 htaccess 文件。有什么方法可以将这 2 个文件的详细信息和副本发送给您吗?
    • 按照你的建议试过了,但还是不行。所以我已经用你所说的所有信息更新了这个问题。非常感谢您的所有帮助。
    • 无论我将新规则放在哪里,被触发的规则都是Login 底部的/community/.htaccess 规则还有其他想法吗?
    • 添加了规则并且效果很好...添加了其他规则作为第一个并且一切都很好...你我的朋友是一个绝对死板的传奇......非常感谢你你所有的帮助......
    • 感谢您的投入和辛勤工作。根据您的观察,我回到了 SSO 开发人员并让他们查看问题。他们已经在 PHP 代码中编写了一个修复程序。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-05
    • 2014-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-24
    相关资源
    最近更新 更多