.htaccess 如果请求不包含特定的字符串模式,则阻止所有请求

fquxozlt  于 2023-04-21  发布在  其他
关注(0)|答案(1)|浏览(144)

我有一个本地化的Laravel网站,对于那个网站,我需要允许下面提到的URL模式的请求。如果不符合这个URL模式,需要导航到404页面。

https://www.sampledomain.ch/
https://www.sampledomain.ch/some_text/de
https://www.sampledomain.ch/some_text/it
https://www.sampledomain.ch/some_text/fr
https://www.sampledomain.ch/de
https://www.sampledomain.ch/it
https://www.sampledomain.ch/fr

有谁能指导我通过htaccess文件来做这个吗?

7bsow1i6

7bsow1i61#

1.)使用.htaccess

带注解代码

# Run this code IF "mod_rewrite" module was enabled
<IfModule mod_rewrite.c>
    # Enable server-side redirects
    RewriteEngine On

    # If the part after the domain is existed (so not just domain)
    #
    # %{REQUEST_URI}: the part after the domain
    # !^/*$: not empty after "/"
    #
    RewriteCond %{REQUEST_URI} !^/*$
    # AND
    # If the part after the domain not end with de, it, or fr
    #
    # %{REQUEST_URI}: the part after the domain
    # !: not
    # /(de|it|fr): /de or /it or /fr
    # $: need end of url after (de or it or fr)
    # [NC]: treats lowercase and uppercase letters equally
    #
    RewriteCond %{REQUEST_URI} !/(de|it|fr)$ [NC]
    # In that case, it returns a 404 error
    RewriteRule ^ - [R=404,L]
</IfModule>

代码

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI} !^/*$
    RewriteCond %{REQUEST_URI} !/(de|it|fr)$ [NC]
    RewriteRule ^ - [R=404,L]
</IfModule>

测试用例

https://www.sampledomain.ch/ # OK (not redirected)
https://www.sampledomain.ch/some_text/de # OK (not redirected)
https://www.sampledomain.ch/some_text/it # OK (not redirected)
https://www.sampledomain.ch/some_text/fr # OK (not redirected)
https://www.sampledomain.ch/some_text/some_text/de # OK (not redirected)
https://www.sampledomain.ch/some_text/some_text/it # OK (not redirected)
https://www.sampledomain.ch/some_text/some_text/fr # OK (not redirected)
https://www.sampledomain.ch/de # OK (not redirected)
https://www.sampledomain.ch/it # OK (not redirected)
https://www.sampledomain.ch/fr # OK (not redirected)

https://www.sampledomain.ch/some_text/DE # OK (not redirected)
https://www.sampledomain.ch/some_text/It # OK (not redirected)
https://www.sampledomain.ch/some_text/rF # OK (not redirected)
https://www.sampledomain.ch/De # OK (not redirected)
https://www.sampledomain.ch/iT # OK (not redirected)
https://www.sampledomain.ch/FR # OK (not redirected)

https://www.sampledomain.ch/FR123 # 404 error
https://www.sampledomain.ch/fr/test # 404 error
https://www.sampledomain.ch/a # 404 error

etc.

2.)使用Laravel路由(更好的选择)

但是,如果你正在使用Laravel,那么也值得重新考虑使用内置路由器!
然而,这个例子要求我们将语言代码移到URL的开头,这通常是一种常见的做法。
1. domain2. language3. any other parameters

Route::get('/{language}/{any}', function ($language, $any) {
    // Handle the request in this function.
    // The $language variable contains the language value,
    // and the $any variable contains any part of the URL.
})->where('any', '.*');

您现在的任务是处理允许的语言。

Route::get('/{language}/{any}', function ($language, $any) {
    $allowedLanguages = ['de', 'en', 'fr'];

    if (!in_array(Str::lower($language), $allowedLanguages)) {
        abort(404);
    }

    // If the language is valid, we can continue with the processing.
})->where('any', '.*');

更多信息:Laravel文档-路由参数编码正斜杠

相关问题