HTaccess - 尾部斜杠 + .php +锚点(ExpressionEngine)
我刚刚从另一个网站管理员的工作中开始了一个项目(我讨厌修补别人的东西::sad::),我有一些问题。首先我们使用表达式引擎 1.x 。
我的问题:.HTACCESS 中有一个尾部斜杠重定向,但我的用户只需要访问一个 .php 页面 (www.mydomain.com/mobile/index.php),但链接被重定向到/index.php/,另一个问题是锚点被更改为相同的方式(www.mydomain.com/somepage/#anchor1) /#anchor1/
所以我的问题是...有没有办法将异常放入尾部斜杠重定向代码中?我的意思是我只需要在几页内修复它。请注意,我们的表达式引擎删除了所有 index.php 以包含 www.mydomaine/contact/、www.mydomaine/about/、www.mydomaine/infos/ 等链接。
目前 htaccess 尾随代码是:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
PS:我们也有一个删除 index.php 的代码:
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
谢谢帮助!
I just started a project from another webmaster work (i hate patching other's stuff ::sad::) and i have some issues. First we are on expression engine 1.x .
My problem: There is a trailing slash redirection in the .HTACCESS, but my users need to have access to only one .php page (www.mydomain.com/mobile/index.php) but the link is redirected to /index.php/, another problem is the anchors are changed to the same way (www.mydomain.com/somepage/#anchor1) to /#anchor1/
So my question is... there is a way to put exception into trailing slash redirection code? I mean i just have to fix it in few pages. Take note that our expression engine remove all the index.php to have links like www.mydomaine/contact/, www.mydomaine/about/, www.mydomaine/infos/ , ect.
Currently the htaccess trailling code is :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
P-S: we have a code that remove index.php too:
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5})$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(members|P[0-9]{2,8}) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
Thx for help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是否存在需要强制执行 URL 中尾随斜杠的正当理由?如果没有,我只需删除重写规则即可解决您的问题:)
大多数 ExpressionEngine URL 都可以工作带有或,不带尾部斜杠。
解决此问题的最简单方法是删除过度热心的尾部斜杠重定向,并将其替换为更宽松的版本:
使用上面的代码,
example.com/mobile/index.php
将不会被重写为example.com/mobile/index.php/
,带有锚点example.com/page/#anchor1
的页面也不会被重写。EE 不知道
/mobile
与/mobile/
等 URI 之间的区别,网络分析应用程序和搜索引擎可能会考虑这些单独的网页。如果您正在开发静态网站,这并不是什么大问题,因为如果您尝试访问前一个 URI(没有尾部斜杠),Apache 会自动将客户端重定向到后者(有尾部斜杠)。但对于像 EE 这样的 Web 应用程序,
index.php
之后的 URI 中的所有内容均由应用程序而不是 Apache 处理,此重定向是留给你。就像决定使用或不使用www
子域一样,您选择强制使用尾部斜杠还是反之亦然并不重要;重要的是您强制执行其中之一。Is there a valid reason trailing slashes in URLs need to be enforced? If not, I would just remove the rewrite rule and your problem is solved :)
Most ExpressionEngine URLs will work with or without trailing slashes.
The easiest fix to this problem would be remove the overzealous trailing slash redirection, and replace it with a more relaxed version:
Using the above code,
example.com/mobile/index.php
will not get rewritten toexample.com/mobile/index.php/
, nor will pages with anchorsexample.com/page/#anchor1
.EE doesn't know the difference between URIs like
/mobile
versus/mobile/
, web analytics apps and search engines may consider these separate web pages. If you're developing a static website, this isn't a big deal, because if you attempt to go to the former URI (sans trailing slash), Apache will automatically redirect the client to the latter (with trailing slash).But for a web application like EE, where everything in the URI after
index.php
is handled by the application rather than Apache, this redirection is left up to you. Just like the decision to use or not use awww
subdomain, it doesn't matter whether you choose to force a trailing slash or vise-versa; it just matters that you enforce one or the other.