Rick Spencer
Rick Spencer

Reputation: 31

htaccess redirect from subdir to root passing referrer as parameter

I'm fairly new to mod rewrite and I am having issues trying to achieve the following for our private forum users. The private forum is in a sub directory of the site called community

user clicks an email link:

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

when browser goes to forum and the user is not logged in. The forum injects /login/ into the {QUERY_STRING} although it does not show in the URL and they are directed to the website login page and this is the only way to tell if the user is logged in or not in relation to the required RewriteRule.

The URL needs to look like this when arriving at the login page:

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

what I have tried is:

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

which gets me this:

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

everything else after the question mark is missing...

If I add %{QUERY_STRING} after the {REQUEST_URI} in the RewriteRule you can then see login has been injected after /community in the {QUERY_STRING} and then a &ref= that contains a Base64 encoded version of the original referring URL from the email.

UPDATE:

The community is a private forum in the community sub directory and there are 2 .htaccess files one in the root and the other in the community dir its self.

I have inherited this and to me the htaccess files look a bit messy We need to make sure all http traffic is forced to https

Root .htaccess file

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

community .htaccess file

# 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
############################################

and the url I am trying to use to test this with is

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

can anybody help me with this?

Cheers,

Upvotes: 1

Views: 163

Answers (1)

anubhava
anubhava

Reputation: 784958

You can use this rule as first rule in your /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

Upvotes: 1

Related Questions