【问题标题】:Not needing index.php from example.com/{lang}/index.php不需要来自 example.com/{lang}/index.php 的 index.php
【发布时间】:2017-02-14 19:54:23
【问题描述】:

我的 .htaccess 目前看起来像:

RewriteEngine On

RewriteCond %{THE_REQUEST} \s/+([^/]+)/\?lang=([^\s&]+) [NC]
RewriteRule ^ /%2/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-z]{2})/([^/]+)/?$ $2/?lang=$1 [L,QSA]

这意味着example.com/{lang}/some-page.php 充当example.com/some-page.php?lang={lang}

剩下的问题:example.com/{lang} 不起作用,除非它有 index.php 像 `example.com/{lang}/index.php

当我们直接去example.com/{lang}时,我怎样才能让它工作?

【问题讨论】:

    标签: php .htaccess


    【解决方案1】:

    这个怎么样:

    RewriteEngine On
    
    RewriteCond %{ENV:REDIRECT_rewritten} !=1
    RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$ [NC]
    RewriteRule ^([^/]*)$ /%1/$1? [R=301,L]
    
    RewriteCond %{REQUEST_URI} !^/js/
    RewriteRule ^([a-z]{2})(?:/([^/]+))?$ $2?lang=$1 [NC,L,QSA,E=rewritten:1]
    

    我真的不喜欢在 THE_REQUEST 上进行匹配,所以改变了它。这也只会重定向您可能不想要的 /?lang=xx。我在您可能不想要的两个规则中对 lang 字符串使用了相同的匹配。告诉我。

    不确定您是否需要文件或目录存在检查,如果没有必要,它会减慢速度,因为每次请求都必须轮询文件系统两次。根目录下有两个字母的文件或目录吗?

    【讨论】:

    • 没有两个字母的文件/目录,所以我们可以省略。使用你的规则但是当我去example.com/xx 时,我得到一个 404
    • 我删除了支票。你把它放在根 .htaccess 文件中吗?
    • 我添加了检查以避免循环。
    • 是的 - 它是根 .htaccess 文件。现在我在访问任何 url 时都会收到内部服务器错误
    • 勾选php.net/manual/en/function.setcookie.php,需要将路径设置为'/',否则只会在设置的目录及其下有效。
    猜你喜欢
    • 2011-12-13
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多