【问题标题】:Apache two RewriteRules dependantApache 的两个 RewriteRules 依赖
【发布时间】:2016-12-11 00:08:39
【问题描述】:

我有一个电子商务网站,我想让它成为多语言。到目前为止,英语已被用作默认语言。下一个 URL 工作正常,已被 Google 编入索引。

* example.com/article/id-item

由于我需要使其成为多语言,因此 url 需要采用这种方式:

* example.com/en/article/id-item -> which is language default
* example.com/fr/article/id-item
* example.com/de/article/id-item

因为我不想被 google 惩罚,因为我当前的 url 被很好地索引,而不是使用 http://example.com/en/article/id-item 我想使用当前的英文 URL,所以我的 URL 应该是这样的:

* example.com/article/id-item -> English
* example.com/fr/article/id-item -> French
* example.com/de/article/id-item -> German

但是在内部,要使用 symfony 正确路由,我需要这些内部重定向(在浏览器地址框中看不到):

* example.com/index.php/en/article/id-item -> English
* example.com/index.php/fr/article/id-item -> French
* example.com/index.php/de/article/id-item -> German

所以我一直在测试我的 .htaccess,但它不能正常工作:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^index\.php
RewriteRule ^(.*)$ index.php/$1 

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^index\.php/(en|fr|de)
RewriteRule ^index\.php(/?.*)$ en/$1 [L]

这可以在:http://htaccess.mwl.be/ 使用 url example.com/article/id-item 作为示例进行测试

结果是: 输出网址

example.com/index.php/article/id-item 调试信息

1 RewriteCond %{REQUEST_FILENAME} !-f 满足这个条件。

2 RewriteCond %{REQUEST_FILENAME} !-d 满足这个条件。

3 RewriteCond %{REQUEST_URI} !^index.php 满足这个条件。

4 RewriteRule ^(.*)$ index.php/$1 新的url是example.com/index.php/article/id-item

5 RewriteCond %{REQUEST_FILENAME} !-f 满足这个条件。

6 RewriteCond %{REQUEST_URI} ^index.php/(en|fr|de) 这个条件不满足。

7 RewriteRule ^index.php(/?.*)$ en/$1 [L] 不符合这条规则。

我不明白为什么在规则 6 中,REQUEST_URI 不是规则 4 的结果。

有人知道会发生什么吗?

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    有了这段代码,我想我已经完成了重写:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_URI} !^/?(en|fr|de)
    RewriteCond %{REQUEST_URI} !^/?index\.php
    RewriteRule ^(.*)$ en/$1 [QSA,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/?index\.php
    RewriteRule ^(.*)$ "index.php/$1" [QSA,L]
    

    但主要问题在于 Symfony/HttpFoundation 请求对象,原因如下: https://github.com/symfony/symfony/pull/15354

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-13
      • 1970-01-01
      • 2021-08-15
      • 2017-04-08
      • 1970-01-01
      • 2020-09-13
      • 1970-01-01
      • 2011-04-26
      相关资源
      最近更新 更多