1

In order to disallow multiple language folder repetition in my website http://domain.com/en/fr/es/ I set the following rule:

RewriteRule ^([a-z]{2}/){2,}(.*)$ /$1$2 [R=301,L]

It's working nice but my problem is that the first captured group return es, but I'd like to get the first language folder en. How can I do that?

anubhava
  • 761,203
  • 64
  • 569
  • 643
Websphere
  • 563
  • 8
  • 24

1 Answers1

1

Change your rule to this:

RewriteRule ^([a-z]{2}/)(?:[a-z]{2}/)+(.*)$ /$1$2 [R=302,L]

Problem in your pattern is that ([a-z]{2}/){2,} matches all of en/fr/es/ and keeps es/ in the captured group.

With my suggested regex we will capture only en/ in first captured group and next non-capturing group will match fr/es/ thus giving use $1=en/ as desired.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • I just didn't understand the `?:`, what is it for? – Websphere Sep 21 '15 at 14:44
  • hi @anubhava, i'll have one last request please: sometimes i have 2 "/" in my URLs and don't know where they come from (site.com/store//category/) so how can I alter the rule you gave me so it can redirect the double slashes to the correct URL ? Thanks – Websphere Dec 08 '15 at 14:12
  • Thanks for the prompt reply ;) I'll try it when i'm back home and will let you know ;) – Websphere Dec 08 '15 at 14:41
  • I have tried: `RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)/+(/[^\s]+) [NC] RewriteRule ^ /%1%2 [R=301,L,NE]` but site.com/store////// returns site.com/store// :/ – Websphere Dec 08 '15 at 18:12
  • 1
    cool, i found the answer which is yours in this [thread](http://stackoverflow.com/questions/20353719/removing-multiple-slashes-with-htaccess) :) thanks again ;) – Websphere Dec 08 '15 at 18:23
  • Hi @anubhava, I have one other question please, and i prefered to ask it here as you have always gave me great and correct answers :D
    I'm using the following directives to hide .php extentions: `RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^(.*)/$ /$1.php` But it seems like the page is sometimes loaded twice! do you see anything wrong? Thanks a lot
    – Websphere Nov 05 '16 at 22:23
  • thanks for your email. I could tell it loads twice because I have a SESSION variable which exists. In the said page (site.com/mypage/) the SESSION variable is unset(). If i do a simple `echo $_SESSION['variable'];` in /mypage/ the variable doesn't show, so it's already unset, but in mypage.php, the SESSION variable is displayed correctly. – Websphere Nov 06 '16 at 16:26
  • 1
    Thank you. It's done: http://stackoverflow.com/questions/40451788/page-loaded-twice-when-hiding-php-extension – Websphere Nov 06 '16 at 16:39
  • hi @anubhava, hope you're doing well. I remember you were an expert in regex :) would you please be able to contribute in this post: https://stackoverflow.com/questions/51170921/redirect-urls-that-do-not-start-with-specific-string ? Thank you very much – Websphere Jul 04 '18 at 11:06
  • Hi again @anubhava, how are you? I'm sorry to bother you, I'm just stuck in a problem and no one seems to be able to help. would you please have a look at this thread: https://stackoverflow.com/questions/52688593/what-is-wrong-with-this-simple-rewrite-rule-that-doesnt-redirect ?? thanks a lot :) – Websphere Oct 08 '18 at 13:45
  • Allow me to chase you up again coz i'm blocked because if this said rule that doesn't work, here they are and maybe it'll take you a minute to find what's wrong: `RewriteRule ^(?![a-z]{2}/)(.*)$ /en/$1 [R=301,L] # redirect to default language RewriteRule ^([a-z]{2})/(.*)$ /$2?lang=$1 [QSA,L]` Thanks a lot – Websphere Oct 09 '18 at 11:11