简体   繁体   中英

Htaccess rewrite url and remove part from url

Hi I'm trying to do the following.

When the domain is accessed through http it should redirect to https. When domain contains www it should redirect to https When domain contains language abbreviation it should remove it.

Possible urls:

1: http://www.example.com/en/how-did-it-all-start

2: http://example.com/en/how-did-it-all-start

3: http://www.example.com/how-did-it-all-start

These domains should redirect with one 301 redirect to https://example.com/how-did-it-all-start

I have tried numerous things but nothing seams to work when I'm trying to combine it with the www. It now only works for http urls, when I add the www to it, it breaks.

RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} ^en/(.*)$ [NC]
RewriteRule ^ https://%1%/%2% [R=301,L]

I don't know how to properly combine those to conditions without breaking it. Tried to do in different ways but until now no success. Any help would be appreciated.

Thanks in advance.

You need to have [OR] clauses in your conditional with optional matches to handle all 3 cases:

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(?:en/)?(.*)$ https://%1/$1 [R=301,L,NE]

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