【问题标题】:removing www and index.php from url in symfony从 symfony 的 url 中删除 www 和 index.php
【发布时间】:2011-12-13 03:25:56
【问题描述】:

我已经搜索了 3 或 4 个小时没有任何结果(在搜索之前我玩了一个小时的规则但无法做到)

我不知道你是否注意到,但谷歌使用 www 是这样的

当它没有子域时,它将是 www.google.com/blabla 和 如果有子域,它将是 earth.google.com/blabla

这是第一部分

第二部分,正如你在 symfony 中所知道的,url 就像 domain.com/index.php/test 一样,感谢 symfony .htaccess 文件,你可以通过 domain.com/test 访问它 所以这就是我努力实现的目标

domain.com/test 重定向到 www.domain.com/test

www.sub.domain.com/blabla 重定向到 sub.domain.com/blabla

www.sub.domain.com/ 重定向到 sub.domain.com(没有任何 index.php XD)

我从 domain.com/ 重定向到 www.domain.com 时遇到的一个恼人问题是,重定向后它就像 www.domain.com/index.php(我讨厌 index.php:P)

那么有没有办法通过一个重定向来解决这个问题? 我敢肯定我不是唯一一个需要这样的东西的人,对于其他将使用 symfony 或其他框架的网站的人来说,这可能是一个想法 谢谢

这是我完整的 htaccess 文件

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
  RewriteEngine On

  # The admin subdomain returns to the backend
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{HTTP_HOST} ^admin\.mydomain\..*
  RewriteRule ^(.*)$ backend.php [QSA,L]

  # uncomment the following line, if you are having trouble
  # getting no_script_name to work
  #RewriteBase /

  RewriteCond %{HTTP_HOST} !^www.mydomain.com$
  RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301]

  # we skip all files with .something
  RewriteCond %{REQUEST_URI} \..+$
  RewriteCond %{REQUEST_URI} !\.html$
  RewriteRule .* - [L]

  # we check if the .html version is here (caching)
  RewriteRule ^$ index.html [QSA]
  RewriteRule ^([^.]+)$ $1.html [QSA]
  RewriteCond %{REQUEST_FILENAME} !-f

  # no, so we redirect to our front web controller
  RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

【问题讨论】:

  • 你的 settings.yml 的 prod 部分是否有 no_script_name: true 和:htaccess 中的 RewriteRule ^(.*)$ index.php [QSA,L]?

标签: apache mod-rewrite symfony1 no-www


【解决方案1】:

在您的 VHOST 配置中:

RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^/(.*) http://domain.com/$1 [R=301,L]

还请注意,从美学的角度来看,您可能更喜欢删除 www.,从技术角度(DNS、cookies 等)来看,最好使用 www. 作为前缀,并在相反的方式。

【讨论】:

  • 感谢您的回答,但它不符合我的要求,我将按照您说的去做(包括所有域的 www,但从 domain.com 重定向到www.domain.com 仍然存在,因为它重定向到 www.domain.com/index.php
  • 如果原始请求包含/index.php,它只会重定向到/index.php。我认为您还缺少其他东西。
  • 不,它不需要 index.php 因为当你输入 domain.com 因为默认页面是 index.php 并且你在 www.domain.com 的末尾有 $1 它会变成 www.domain.com/index.php
  • 也许您的重写规则顺序错误。这一个应该几乎在顶部。如果您需要更多支持,请发布您的整个 .htaccess。
  • 我无法发布我的整个 htaccess,因为 cmets 似乎有字符限制
猜你喜欢
  • 2015-09-09
  • 2013-10-23
  • 2012-08-15
  • 2016-08-20
  • 1970-01-01
  • 1970-01-01
  • 2017-09-14
  • 2018-09-18
  • 2013-06-29
相关资源
最近更新 更多