Home > Blockchain >  Redirect root of the subfolder but not the folders under that with .htaccess
Redirect root of the subfolder but not the folders under that with .htaccess

Time:11-28

I have a Modx site with multiple languages. I am trying to do a redirect for the url

https://example.com/fi/

to

https://example.com/fi/something

But I only want to redirect the root of the "subfolder" not anything under that eg. "example.com/fi/products" or anything else. Just the root /fi/

The current htaccess file is as follows

RewriteEngine On
Options  FollowSymLinks
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /

#Check for Fi
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteCond %{HTTP:Accept-Language} ^fi [NC]
RewriteRule ^$ http://%{HTTP_HOST}/fi/ [L,R=301]

# Default to En
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^$ https://%{HTTP_HOST}/en/ [L,R=301]

# Fix current links

RewriteCond %{HTTP_HOST} example.com [NC]
RewriteCond %{REQUEST_URI} !/en
RewriteCond %{REQUEST_URI} !/fi
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /en/$1 [L]

# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

If I try to

RedirectMatch 301 /fi /fi/something

I end up with endless loop of /fi/something in the URL

CodePudding user response:

The rewrite loop exists because your target URL /fi/something obviously again is matched by your pattern /fi ...

Actually you don't want to use RedirectMatch here at all, but a simple RewriteRule:

RewriteRule ^/?fi/?$ /fi/something [R=301,L]

You want to add that rule to the existing configuration file above the comment # Fix current links.

  • Related