【问题标题】:Joomla: Permanently redirect from example.com/index.php/foo/bar to example.com/foo/bar (no index.php in URL)Joomla:从 example.com/index.php/foo/bar 永久重定向到 example.com/foo/bar(URL 中没有 index.php)
【发布时间】:2020-11-15 12:24:00
【问题描述】:

.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

通过此设置,页面/foo/bar 可以在漂亮的 URL example.com/foo/bar 下访问,而丑陋的 URL example.com/index.php/foo/bar 仍然有效。我需要实现的是从example.com/index.php/foo/barexample.com/foo/bar 的永久重定向。

RewriteRule .* index.php [R=301,L] 不起作用。

RewriteRule (.*) index.php/$1 [R=301,L] 正好相反,它从example.com/foo/bar 重定向到example.com/index.php/foo/bar。请帮帮我!

【问题讨论】:

  • 根据 this meta postthis meta post,这个问题对于 SO 来说根本不是题外话,所以近距离投票不符合全民公投。
  • 我也非常不同意关闭页面的奇怪理由。
  • 不,这个问题属于这里,它是关于重写规则,即mod_rewrite,并且不是 Joomla 特定的。这些重写规则可以在没有 Joomla 的任何本土网站中使用
  • 比在平台之间移动问题更好,在Joomla论坛上添加一段与重写规则相关的Joomla问题的有用链接会更好。正如我提议的那样,如果您没有任何异议,我自己会这样做(因为我是那里的新用户)
  • @mickmackusa 让我的问题迁移到 JSE?不,谢谢!

标签: apache .htaccess redirect mod-rewrite url-rewriting


【解决方案1】:

我建议这样写:

RewriteEngine On
RewriteBase /

# remove index.php from / OR any /dir
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]

# redirect rule to remove /index.php/ from start
RewriteRule ^index\.php(/.*) $1 [L,R=301,NC]

RewriteCond %{REQUEST_URI} !^/index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

在新规则中,我们使用正则表达式^index\.php(/.*) 匹配一个模式,该模式匹配一​​个以/index.php/ 开头后跟零个或多个任意字符的URI。 (/.*) 是我们的匹配项,/index.php 之后的捕获组用作反向引用,即目标中的$1(我们在组#1 中捕获的字符串),以根据需要在/index.php 之后获取 URI。

参考资料:

【讨论】:

  • 这就是我在答案中包含参考链接的原因。 mod_rewrite 规则有这个 DSL。 RewriteRule <pattern> <action/target> <flags>。所以目标是我们正在重定向/重写的东西。
  • 我需要更改什么才能另外将example.com/index.php 重定向到example.com
  • DSL = domain-specific language?
  • 1.是的DSL = domain-specific language。 2. 是的RewriteRule ^RewriteRule .* 效率略高。我正在添加一条规则,将example.com/index.php 重定向到example.com/
  • @ThreeYearOld:我不是 Joomla 专家,但您可以尝试在该规则之前添加 RewriteCond %{THE_REQUEST} /index\.php/ 条件。如果您仍有问题,请随时使用 Joomla 标签提出新问题
猜你喜欢
  • 2011-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 2011-12-13
  • 2014-10-17
相关资源
最近更新 更多