1

I need help to write proper rewrite rules in my htaccess files.

I need to redirect something like fr.example.com to example.com/fr, because we recently changed the whole website and the multilingual system is managed differently. The structure and the pages too.

I managed to do that successfully with this piece of code:

RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]

My problem now is to write something more specific for pages, for example :

fr.example.com/discover/foo should go to example.com/fr/bar/foo (different path, nothing consistant)

BUT ! example.com/discover/foo should go to example.com/bar/foo (end of the url is the same in both english and french)

Right now, since I have some common 301 redirects, the french urls aren't redirect properly and lead to the english pages. For example that one :

Redirect 301 /discover/foo /bar/otherfoo

Successfully redirects example.com/discover/foo to example.com/bar/otherfoo but also redirects fr.example.com/discover/otherfoo

How can I write two different rules for english and french? I'll have to write a bunch of different rules since everything is very different from the old subdomain to the new directory, I don't mind.

Thanks !

EDIT

Please note that it's for a wordpress installation, and the htaccess starts with :

RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

1 Answer 1

1

First the these rules:

RewriteCond %{HTTP_HOST} ^fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]

should look like this :

RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC]
RewriteRule (.*) http://example.com/fr/$1 [L,R=301]

In order to capture bot www & non-www requests for subdomain.

Also this rule :

Redirect 301 /discover/foo /bar/foo

Will capture both requests to domain and sub-domains and using mod_rewrite here is correct not mod_alias so , replace this line with :

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteRule ^discover/foo http://example.com/bar/foo [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC]
RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301]

Note: clear browser cache then test.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you ! But regarding the two last lines, that would redirects anything after discover/ to bar/ right? How can I be more specific? I'm updating my example because sometimes, the slug might be different (and not stay "foo")
@CécileHabran i was thinking that you want to replace any discover with bar , test it now
Mh, that works for the english (without the subdomain) but not with fr. I removed all the 301 redirects from the htaccess, but it just display the 404. The first redirect RewriteCond %{HTTP_HOST} ^(www\.)?fr\.example\.com [NC] RewriteRule (.*) http://example.com/fr/$1 [L,R=301] works tho
I cleared it already but I did it again and tested in another browser. The RewriteCond %{HTTP_HOST} ^(www\.)?(fr)\.example\.com [NC] RewriteRule ^discover/foo http://example.com/%2/bar/foo [L,R=301] doesn't do anything :/
I edited the question since it might be usefull it's a wordpress installation
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.