Ismail H
Ismail H

Reputation: 4499

Regex to prefix URI in apache RewriteRule and execute a find/replace

I'm trying to prefix all URI in the apache vhost conf by "redirect-lang" and change the "/" to "%". For example :

http://example.com/category/article/how/to/redirect

Will become:

http://example.com/redirect-lang/category%article%how%to%redirect

To do so, I wrote the following :

RewriteRule (.*)([^\/]+)\/([^\/]+)\/?$ http://%{HTTP_HOST}$1$2-$3
RewriteCond %{REQUEST_URI} ^(?!(.*)(\/?redirect)) [NC]
RewriteRule (.*)$ http://%{HTTP_HOST}/redirect/$0 [L]

The first RewriteRule Works perfectly alone in replacing the "/" with "%", but when adding the RewriteCond and the second RewriteRule, it goes in an infinite loop.

Anyone to help me with this ?

Upvotes: 1

Views: 1456

Answers (1)

anubhava
anubhava

Reputation: 785631

To rewrite each URL with prefix /redirect-lang/ use this rule as very first rule:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!redirect-lang/).*)$ redirect-lang/$1 [L,NC]

Upvotes: 1

Related Questions