重写规则检查字符串是否存在

时间:2021-09-14 05:04:44

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

我需要一个RewriteRule,它将http://domain.org/foo/bar的网址更改为http://domain.org/de/foo/bar,但如果有像http://domain.org这样的网址则不会执行任何操作/ en / foo / bar或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

所以,如果没有en /并且没有de /那么它应该添加de /,否则它什么也不做

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/.

如何才能做到这一点?我已经玩了一些正则表达式,但我不知道如何检查是否有/或如果没有en /。

help is highly appreciated.

帮助非常感谢。

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

更新:我当前的规则是运行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.

这是wordpress和de /和en /不是实际文件夹..它们由多语言插件使用。因为我有这个插件的一些问题,我需要如上所述的重定向。

2 个解决方案

#1


You can use this rule in your root .htaccess:

您可以在根目录中使用此规则.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.

在默认WP规则之前保留此规则。


EDIT Full .htaccess:

编辑完整.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

#2


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

通常没有任何其他规则,这将重定向任何不以/ 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]

#1


You can use this rule in your root .htaccess:

您可以在根目录中使用此规则.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.

在默认WP规则之前保留此规则。


EDIT Full .htaccess:

编辑完整.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

#2


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

通常没有任何其他规则,这将重定向任何不以/ 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]