I'm trying desperately to figure out how to redirect root only, without redirecting (literally) anything after the slash / (including anchors #).
I have a landing page I'd like to redirect all domainname.com traffic too, but also need to let people enter the main site by using deep links like domainname.com/store, domainname.com/#about, or domainname.com/#up (the main page & Nav use anchors).
Code I'm using is below and results are:
- redirects domainname.com > landingpage.domainname.com (GOOD)
- does not redirect domainname.com/store (GOOD)
- redirects domainname.com/#about > landingpage.domainname.com/#about (BAD - THIS IS THE PROBLEM)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com [NC]
RewriteRule ^$ http://landingpage.domainname.com [L,NC,R=301]
Thanks,
James
James
do you have the option of removing your resources from domain.com/store to sub.domain.com/store? It seems like you'll be doing a huge amount of redirecting, which will become unwieldy over time.
Anyways, I would just rewrite the domain.com requests, and leave all the others alone.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^ http://www.example.com [NC]
RewriteRule ^(.*)$ http://sub.example.com/ $1 [R=301,L]
You can't use htaccess to do this, because the #about
part of the URL is never sent to the server , and if it's never sent to the server, all apache and mod_rewrite sees is /
, without the #about
. The #about
is a URL fragment and stays strictly on the browser's side. There's no way to prevent this redirect using mod_rewrite.
Therefore, you'll have to write javascript to do the redirect instead and get rid of the redirect in your htaccess file.
<script language=”javascript” type=”text/javascript”>
if(window.location.hostname != "landingpage.domainname.com" && // check that hostname isn't landingpage
!window.location.hash && // check that there is no fragment
window.location.pathname == "/") {
window.location = "http://landingpage.domainname.com/";
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.