【问题标题】:How do i redirect user if user enter the URL with /index.php to without having /index.php in the url如果用户使用 /index.php 输入 URL 而不在 url 中包含 /index.php,我如何重定向用户
【发布时间】:2013-03-07 10:20:49
【问题描述】:

我在我的网站中遇到了 URL 重定向的小问题。这是我的 .htaccess 文件:

RewriteEngine On 

# Start –301 redirect for "http://" to "http://www"

Options +FollowSymLinks

rewritecond %{http_host} ^example.com [nc]

rewriterule ^(.*)$ http://www.example.com/$1 [r=301,L]

RewriteEngine On 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /?(.*)/index\.php
RewriteRule ^(.*)$ /%1/ [L,R=301]

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

这很好用。所有 URL 都可以完美运行,而无需在其中包含 /index.php。

但我的问题是,当有人使用 index.php (例如 https://example.com/index.php/test )放置 URL 时。所以它也会起作用。这是一个问题,我希望当用户将 /index.php 与 URL 一起放置时,它会重定向到同一页面,而其中没有 /index.php。请帮我解决这个问题。

【问题讨论】:

  • 为什么用户要在 URL 中附加 index.php?即使他们这样做了,为什么还要费心“修复”它呢?通常只有黑客才愿意破坏 URL。
  • @TimS。如果有人将 url 与 /index.php 放在一起,则会产生问题,它将在 google 中标记为重复的 url。如果您有任何建议,请帮助我
  • 不,它不会标记重复。如果您没有在您的网站中放置任何包含index.php 版本的链接,Google 抓取工具不会将其编入索引。
  • @TimS。但是如果有人尝试使用 index.php 访问 url 怎么办?
  • 那么它就会按预期工作,而 Google 甚至都不知道。

标签: .htaccess mod-rewrite joomla joomla2.5


【解决方案1】:

尝试将这个添加到您已有的规则之上:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /?(.*)/index\.php
RewriteRule ^ /%1/ [L,R=301]

如果你尝试匹配请求 URI,它会导致一个循环,因为你已经拥有的规则将请求路由到 index.php,重写请求 URI index.php 从而导致它在重写引擎循环时匹配。

【讨论】:

  • 这会将其重定向到主页。是否可以在没有 /index.php 的情况下将其重定向到相同的请求页面?
  • 我已经更新了我的问题,正如您所说,将您的代码置于我的规则之上。如果我这样说,它会将我重定向到主页http://example.com//,那么它有什么问题。请给我建议。
猜你喜欢
  • 2020-07-07
  • 1970-01-01
  • 1970-01-01
  • 2015-03-15
  • 2012-03-18
  • 1970-01-01
  • 2022-11-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多