beta
beta

Reputation: 5686

rewrite rule check for string existence

i need a RewriteRule which changes an url of http://domain.org/foo/bar to http://domain.org/de/foo/bar but does nothing if there is an url like http://domain.org/en/foo/bar or http://domain.org/de/foo/bar

so if there is no en/ and no de/ then it should add de/, else it should do nothing

how can this be done? i already played around a bit with regex, but I dont know how to check if there is en/ or if there is no en/.

help is highly appreciated.

UPDATE: my current rules are the default rules you should have when running wordpress:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

UPDATE2:

this is wordpress and de/ and en/ are not actual folders.. they are used by a multilanguage plugin. since i have some issues with this plugin i need the redirect as described above.

Upvotes: 1

Views: 256

Answers (2)

anubhava
anubhava

Reputation: 785376

You can use this rule in your root .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(?:en|de)).*)$ de/$1 [L,NC,R]

Keep this rule before default WP rule.


EDIT Full .htaccess:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!(?:en|de)).*)$ de/$1 [L,NC,R]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

Upvotes: 1

Panama Jack
Panama Jack

Reputation: 24458

In general without any other rules, this would redirect anything that doesn't start with /en/foo/bar.

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/en [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /de/$1 [R=301,L]

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

Upvotes: 2

Related Questions