简体   繁体   中英

Wildcard redirect links going to root to subfolder on same domain

I have moved a website from www.example.com to www.example.com/subfolder and using an .htaccess file, I would like to redirect old links going to www.example.com to www.example.com/subfolder. My current .htaccess looks like this:

RewriteEngine on
RewriteCond %{HTTP_HOST} example\.com [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /subfolder/$1 [L]

I've also tried the following:

#RewriteCond %{REQUEST_URI} !^/subfolder/
#RewriteRule ^.*$ http://www.example.com/subfolder%{REQUEST_URI} [R=301,QSA,L]

I keep getting redirect loops or the redirect not working and leading to an error 404.

are you using wordpress? Or you just moving in general?

If you are moving in general this one-liner will work

RedirectMatch ^/$ /subfolder/

Original answer from here

This is another solution i'm using currently.

Do not copy and paste . You need to Replace DOMAIN (without the .com) and SUBFOLDER to the name u want.

RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_URI} !^/subfolder/

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.

# Rewrite all those to insert /folder
RewriteRule ^(.*)$ /subfolder/$1 [L]

You can use this rule in site root .htaccess (parent directory of subfolder ) as the first rule :

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!subfolder/)(.*)$ /subfolder/$1 [L,NC,NE,R=301]

# remaining rules go here

Make sure to clear your browser cache completely when testing this change.

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