从 symfony 中的 url 中删除 www 和 index.php

发布于 2024-12-11 05:54:09 字数 1622 浏览 2 评论 0原文

我已经搜索了大约 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>

I've been searching for like 3 or 4 hours without any result(before searching I played with rules for an hour but couldn't do it)

I don't know if you've noticed or no but google uses www like this

when it has no subdomain it will be www.google.com/blabla and
when there is a subdomain it will be earth.google.com/blabla

This is the first part

And the second part, as you know in symfony urls are like domain.com/index.php/test and thanks to the symfony .htaccess file you can access it via domain.com/test
So here is what I tried so hard to achieve

domain.com/test redirect to www.domain.com/test

www.sub.domain.com/blabla redirect to sub.domain.com/blabla

www.sub.domain.com/ redirect to sub.domain.com (without any index.php XD)

One of the annoying problems I had was redirecting from domain.com/ to www.domain.com was that after redirect it was like www.domain.com/index.php (And I hate index.php :P)

So is there any way with one redirect solve this problem?
I'm sure I'm not the only one who needs something like this and might be an idea for other people who are going to have their site with symfony or other frameworks
Thanks

Here is my complete htaccess file

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2024-12-18 05:54:09

在您的 VHOST 配置中:

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

另请注意,从美观的角度来看,您可能更愿意删除 www.,从技术角度(DNS、cookies...)来看,最好添加 www. 前缀,然后重定向以相反的方式。

In your VHOST config:

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

Also note that from a esthetical point of view you might prefer to remove the www., looking from the technical angle (DNS, cookies, ...), it is always better to prefix with www., and redirect in the opposite way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文