htaccess从subdir重定向到root,将referrer作为参数

时间:2017-09-15 14:22:13

标签: .htaccess mod-rewrite

我对mod重写相当新,我在尝试为我们的私人论坛用户实现以下内容时遇到问题。私人论坛位于名为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中的%{QUERY_STRING}之后添加{REQUEST_URI},您可以看到login/community已注入{QUERY_STRING},然后&ref= {1}}包含电子邮件中原始引用网址的Base64编码版本。

更新

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

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

Root .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/

任何人都可以帮我这个吗?

干杯,

1 个答案:

答案 0 :(得分: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