htaccess redirect / remove characters from the middle of urls / cut ending and put it in the begining

StackOverflow https://stackoverflow.com/questions/22122487

Pergunta

We are having a htaccess redirect problem, hoping to receive a solution. We need to redirect urls (generated mostly after internal search returns) similar to:

domain.com/pretty-much-any-char-1/.../pretty-much-any-char-N/?folder=fr
domain.com/pretty-much-any-char-1/.../pretty-much-any-char-N/?folder=es
domain.com/pretty-much-any-char-1/.../pretty-much-any-char-N/?folder=cn
domain.com/pretty-much-any-char-1/.../pretty-much-any-char-N/?folder=pt
domain.com/pretty-much-any-char-1/.../pretty-much-any-char-N/?folder=ja

to

domain.com/fr/pretty-much-any-char-1/.../pretty-much-any-char-N/
domain.com/es/pretty-much-any-char-1/.../pretty-much-any-char-N/
domain.com/cn/pretty-much-any-char-1/.../pretty-much-any-char-N/
domain.com/pt/pretty-much-any-char-1/.../pretty-much-any-char-N/
domain.com/ja/pretty-much-any-char-1/.../pretty-much-any-char-N/

It would be noteworthy to let you know that we also have the following rule already in .htaccess:

RewriteRule ^(fr|es|cn|pt|ja)/(.*) $2?folder=$1 [L,QSA,NC]

Thanks for your time!

Foi útil?

Solução

Try this

RewriteCond %{QUERY_STRING} folder=(.*)$
RewriteRule ^domain.com(.*)$ domain.com/%1$1?
RewriteRule ^(fr|es|cn|pt|ja)/(.*) $2?folder=$1 [L,QSA,NC]

Edit:

RewriteRule ^(?:www\.)?domain.com(.*)$ www.domain.com/%1$1?

That one works for "www.domain.com" and "domain.com"

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top