Home > Net >  Only top-level domain redirect using .htaccess
Only top-level domain redirect using .htaccess

Time:07-05

I have some htaccess that looks like:

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(?:www\.)?joshrodg\.com$ [NC]
RewriteRule ^ https://joshrodgers.com/$1 [R=301,L,NE]

The first three lines are forcing all of my sites to be https, so we're all good there. The last two lines, I would like to take the non-www and the www of joshrodg.com and redirect to https://joshrodgers.com/.

My code is working, the thing is, it's working a little too good. I use other paths from joshrodg.com like joshrodg.com/example or joshrodg.com/test but I don't want these paths to redirect. I just want the top-level domain to redirect. I'm thinking this is just a quick tweak and I've tried several pieces of code, but am still getting redirected.

Any ideas?

Thanks,
Josh

CodePudding user response:

With "just the top level domain" you actually mean requests to the root folder or to the path "/" only. So you need to match exactly that path only:

RewriteRule ^/?$  https://joshrodgers.com/ [R=301,L]
  • Related