【问题标题】:What RewriteRule shall I add to my existing rules to fix this?我应该在现有规则中添加什么 RewriteRule 来解决这个问题?
【发布时间】:2015-09-30 02:27:44
【问题描述】:

我的页面有 2 种语言(在不久的将来会是 3 种)。它在内页上有这样的结构:

domain.com/en/page (where page is page.php in main root)

可以通过domain.comdomain.com/en/访问主页

在我的服务器中,所有文件都在主根目录下,还有一个名为 items 的文件夹,里面还有一些其他文件。

当我访问像domain.com/en/articles 这样的文件夹文章索引时,就可以了。

但是当我像domain.com/articles 一样访问它时,它会将我带到 404 页面。

如果前面没有/en//el/,我如何才能打开该文件夹?

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(en|el)/articles$ articles/index.php?lang=$1 [NC]

RewriteRule ^(en|el)(/)?$ index.php?lang=$1 [NC,L]

RewriteRule ^(en|el)/(.*)?$ $2.php?lang=$1 [NC,L]

RewriteRule ^([^\.]+)$ $1.php [NC,L]

另外,由于我是 htaccess 新手,请查看我的整体代码并告诉我任何改进。

【问题讨论】:

    标签: php regex .htaccess mod-rewrite url-rewriting


    【解决方案1】:

    你可以有这样的规则:

    RewriteRule ^articles/?$ articles/index.php [NC,L]
    
    RewriteRule ^(en|el)/articles$ articles/index.php?lang=$1 [NC,L,QSA]
    
    RewriteRule ^(en|el)/?$ index.php?lang=$1 [NC,L,QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(en|el)/(.+)$ $2.php?lang=$1 [NC,L,QSA]
    

    【讨论】:

    • 您好,它有效,谢谢!一个问题。如果我访问 domain.com/articles 它会添加一个斜杠,这是因为它是一个文件夹吗?
    • 是的,没错。 mod_dir 模块在目录前面添加一个斜杠。
    猜你喜欢
    • 1970-01-01
    • 2021-01-14
    • 2019-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 2022-11-30
    • 2021-08-18
    相关资源
    最近更新 更多