Htaccess 非 www 并以一种很好的方式剥离 index.php

发布于 2024-09-27 09:55:02 字数 1168 浏览 1 评论 0原文

我的 .htaccess 重定向遇到一些问题。

我想要一种将(非 www)domain.tld 重定向到 www.domain.tld 的情况。我想重写参数以跳过index.php,将/foo 的请求转到index.php/foo。

初始情况

首先我有这些规则

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\ /:-]*)$ index.php [L]

并且这有效。大多。不起作用的是,在 PHP 中 $_SERVER['PATH_INFO'] 保持为空,而且我不喜欢将字符列入白名单。

更改 PATH_INFO 并接受更多

所以我将最后一行更改为:

RewriteRule ^(.*)$ index.php/$1 [L]

这修复了 PATH_INFO 和有限的字符。但是,我最近注意到这导致非 www 重定向到 www。惨败.. 当转到非 www 域时,Apache 说

Moved Permanently
The document has moved here.

Where 'here' is linked to the same thing I type (non-www domain.tld) ,因此无法为用户提供服务。

继续搜索..

我在这里和其他地方发现了很多问答或非 www 重定向的主题,但似乎都以某种方式失败了。例如:

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

这并没有起到多大作用。尽管该网站是在非 www 上提供的,但没有任何内容被重定向。

有人知道我做错了什么或有解决方案吗? :)

(最好,我希望非 www 重定向是全局的。这样我就不必每次都更改实际的域名。)

I'm having some trouble with my .htaccess redirections.

I want a situation in which the (non-www)domain.tld is redirected to the www.domain.tld. And I want to rewrite the arguments to skip the index.php, making a request for /foo go to index.php/foo.

Initial situation

First I had these rules

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_\ /:-]*)$ index.php [L]

And this worked. Mostly. What didn't work was that in PHP $_SERVER['PATH_INFO'] stayed empty and I disliked the whitelisting of the characters.

Change for PATH_INFO and to accept more

So I changed the last line into this:

RewriteRule ^(.*)$ index.php/$1 [L]

This fixed the PATH_INFO and the limited characters. However, I recently noticed that this caused the non-www redirect to www. to fail miserably.. When going to the non-www domain Apache says

Moved Permanently
The document has moved here.

Where 'here' is linked to the same thing I typed (non-www domain.tld) and thus failing to serve the user.

Continuing the search..

I found a lot of Q&A here and elsewhere or the topic of non-www redirections, but all seem to fail in some way. For example:

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

This just didn't do that much. Nothing got redirected, although the website was served on the non-www.

Anyone knowing what I do wrong or having a solution for this mess? :)

(Preferably, I would like the non-www redirection to be global. So that I don't have to change the actual domain name every time.)

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

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

发布评论

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

评论(1

你的往事 2024-10-04 09:55:02

我猜你只是缺少 L 标志来结束重写过程:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

并确保将此规则放在那些只会导致内部重写的规则前面。

I guess you’re just missing the L flag to end the rewriting process:

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

And make sure to put this rule in front of those rules that just cause an internal rewrite.

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