【问题标题】:URL without www to www - using mod_rewrite and CodeIgniter没有 www 到 www 的 URL - 使用 mod_rewrite 和 CodeIgniter
【发布时间】:2015-06-26 19:53:09
【问题描述】:

我正在维护一个大部分由另一个开发团队开发的网站。我正在开发新功能,但有时我不知道某些旧代码究竟是如何工作的。该网站是使用 CodeIgniter 开发的,并且有一个包含单个规则的简单 .htaccess 文件。我不太擅长 PHP 或 mod_rewrite,所以我需要你的帮助。

在 .htaccess 文件中有以下几行:

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

我在 Google 上搜索 mod_rewrite,我想上面的代码检查请求的内容是否不是文件或目录,如果不是,它会重写在域之后插入 index.php?/ 的 URL。例如,如果我请求http://www.example.net/teste,它将被重写为http://www.example.net/index.php?/teste。我对吗?我还认为它是为了匹配包含框架中 index.php 的 CodeIgniter 的 URL 格式,对吧?但我觉得奇怪的是,即使在 RewriteRule 终止执行之后,我也无法在 URL 中看到 index.php?/(但这很好,这正是我想要的)。

我现在的问题是我想将所有不带 www 的 URL 重定向到带 www 的 URL。所以,我尝试在 .htaccess 的最后插入这两行:

RewriteCond %{HTTP_HOST} ^example.net
RewriteRule ^ http://www.example.net%{REQUEST_URI} [R=301]

它有效,但现在我可以在 URL 中看到 index.php?/ 部分。我不想显示 index.php?/ 部分。

我做错了吗?你知道一种在不显示 index.php 的情况下在 URL 中包含 www 的方法吗?/ 部分。

谢谢。

===================

更新答案:

大家好,

我将 .htaccess 修改为如下所示:

RewriteEngine On
RewriteBase /

# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]

# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

RewriteCond %{HTTP_HOST} ^vesteer\.com\.br$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

成功了。现在一切正常。

感谢大家的回复。

【问题讨论】:

  • 我面临的问题的一个例子。如果我访问:“example.net/teste”,我将被重定向到:“www.example.net/index.php?/teste”,但我想要被重定向到:“www.example.net/teste” "
  • 嗨,anubhava,你的意思是改变规则的顺序吗?我试过了,但没有用。同样的问题。
  • 当我更改顺序时,在 Firefox 中出现同样的问题,在 Chrome 中不会发生重写并且图像不再出现。
  • 嘿,anubhava,我找到了一个帮助我解决问题的链接。我无法将答案作为帖子发布,因为我收到了以下消息:“我们不再接受来自此帐户的答案”。但是我在页面上留下了一些 cmets 来解释我是如何解决这个问题的。谢谢。

标签: apache .htaccess mod-rewrite rewrite


【解决方案1】:

好的,找到您的解决方案,只需更新您的 .htaccess 代码

RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]

【讨论】:

  • 您好 Softbazz,感谢您的回复。如果我错了,请纠正我,但是在您发布的 RewriteCond 中,您正在检查 URL 是否以 www 开头,并且我想重写不以 www 开头的 URL。我想把非www改写成www。
  • @Bruno:所以你的意思是像一些子域链接 asif.softbazz.com 到 www.asif.softbazz.com
  • Softbazz,我需要将 www 放在域中,而不是子域中。 example.net 成为 www.example.net。我无法将我的答案发布为帖子,因为我收到此消息:“我们不再接受来自此帐户的答案。”。但如果其他人有同样的问题,我会把我的答案作为 cmets 放在下面。
  • 大家好,我找到了这个链接 ellislab.com/expressionengine/user-guide/urls/… 并修改了我的 .htaccess 使其看起来像这样: RewriteEngine On RewriteBase / # Removes index.php from ExpressionEngine URLs RewriteCond %{THE_REQUEST} ^GET.*index \.php [NC] RewriteCond %{REQUEST_URI} !/system/.* [NC] RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L] # Directs所有 EE web 请求通过站点索引文件 RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ /index.php/$1 [L]
  • RewriteCond %{HTTP_HOST} ^example\.net$ [NC] RewriteRule ^%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 它起作用了。现在一切正常。谢谢大家的回复。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-27
  • 2021-03-25
  • 1970-01-01
  • 2015-02-11
  • 2016-05-11
  • 2010-10-06
相关资源
最近更新 更多