简体   繁体   中英

.htaccess redirect root domain to sub directory

I have an interesting .htaccess config on my ROOT that looks like this:

RewriteEngine On
    # add a trailing slash if portal-pages/$1 is a directory
    RewriteCond %{DOCUMENT_ROOT}/portal-pages/$1 -d

    RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [L,R=301,NE]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(?!portal-pages/)(.*)$ portal-pages/$1 [L,NC]

So my real path would be something like this:

 local.com/portal-pages/home

but the .htaccess translates this path to this:

 local.com/home

it works great, but if I hit local.com I want it to forward to local.com/home. if I hit local.com/whatever that just goes to /whatever.

How can I do this with this configuration? Thanks in advance.

also, how can I force everything to https?

You can use one additional rule to rewrite landing page to /home another redirect rule for http -> https :

RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

# add a trailing slash if portal-pages/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/portal-pages/$1 -d
RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [L,R=301,NE]

# forward landing page to /home
RewriteRule ^/?$ /home [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!portal-pages/)(.*)$ portal-pages/$1 [L,NC]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM