Hey folks, I’m stuck with an htaccess redirect problem. I need to forward a Hubspot URL to our CMS, but I’m not sure how to do it right.
Here’s what I want:
From:
hs.mysite.example.com/webapp/signin?param=view&id=123456&cat=&color=7890
To:
mysite.example.com/webapp/#/signin?param=view&id=123456&cat=&color=7890
The tricky part is I want to redirect just this part:
hs.mysite.example.com/webapp/signin?
to
mysite.example.com/webapp/#/signin?
And keep all the query stuff that follows. I tried this, but it’s not working:
RewriteEngine On
RewriteRule ^hs.mysite.example.com/webapp/signin?* mysite.example.com/webapp/#/signin?$0 [L,NE,NC,R=301]
I’m pretty new to htaccess. Can anyone help me figure this out? Thanks!
yo sophialee92, here’s another way to tackle it:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hs.mysite.example.com [NC]
RewriteRule ^webapp/signin(.*)$ http://mysite.example.com/webapp/#/signin$1 [R=301,L]
this should grab the whole url n redirect it properly. dont forget to test it out n clear ur cache. lemme know if u need more help!
Have you considered using mod_rewrite’s QSA flag? It might simplify your rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hs.mysite.example.com$ [NC]
RewriteRule ^webapp/signin$ http://mysite.example.com/webapp/#/signin [L,R=301,QSA]
The QSA (Query String Append) flag automatically appends the original query string to the redirected URL. This way, you don’t need to manually handle query parameters.
Remember to clear your browser cache when testing, as old redirects can persist. Also, double-check your SSL configuration if you’re using HTTPS. Let us know if this works for you.
hey sophialee92, i think ur almost there! try tweaking ur rule like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hs.mysite.example.com$ [NC]
RewriteRule ^webapp/signin(.*)$ http://mysite.example.com/webapp/#/signin$1 [L,R=301,NE]
this should catch the subdomain n redirect w/ query params intact. lmk if it works!
I’ve dealt with similar redirects before, and here’s what worked for me:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hs.mysite.example.com$ [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^webapp/signin$ http://mysite.example.com/webapp/#/signin?%1 [L,R=301,NE]
This approach uses two RewriteCond directives to match the subdomain and capture the query string. The RewriteRule then redirects to the new URL, preserving all query parameters.
Make sure to test thoroughly, as redirects can be tricky. You might need to adjust based on your specific server setup. Also, consider using a 302 (temporary) redirect for testing before switching to 301 (permanent).
I’ve encountered similar redirect challenges before, and here’s a solution that might work for you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^hs.mysite.example.com$ [NC]
RewriteCond %{REQUEST_URI} ^/webapp/signin [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^ http://mysite.example.com/webapp/#/signin?%1 [L,R=301,NE]
This approach uses multiple RewriteCond directives to ensure we’re only catching the specific URL you want to redirect. The last line uses %1 to preserve all query parameters.
One thing to keep in mind: the hash (#) in the target URL might cause issues with some servers. If you encounter problems, you might need to URL encode it (%23).
Also, make sure to test this thoroughly in a staging environment before implementing it on your live site. Redirects can sometimes have unexpected consequences if not set up correctly.