HTaccess - 尾部斜杠 + .php +锚点(ExpressionEngine)

发布于 2024-11-24 01:10:31 字数 890 浏览 2 评论 0原文

我刚刚从另一个网站管理员的工作中开始了一个项目(我讨厌修补别人的东西::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 技术交流群。

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

发布评论

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

评论(1

深爱不及久伴 2024-12-01 01:10:31

是否存在需要强制执行 URL 中尾随斜杠的正当理由?如果没有,我只需删除重写规则即可解决您的问题:)

大多数 ExpressionEngine URL 都可以工作带有,不带尾部斜杠。

解决此问题的最简单方法是删除过度热心的尾部斜杠重定向,并将其替换为更宽松的版本:

<IfModule mod_rewrite.c>
    # Enable Apache's RewriteEngine
    RewriteEngine On

    # Add Trailing Slashes to URLs
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+[^/])$ /$1/ [R=301,L]

    # ExpressionEngine Remove index.php from URLs
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule> 

使用上面的代码,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 子域一样,您选择强制使用尾部斜杠还是反之亦然并不重要;重要的是您强制执行其中之一。

旁注:在 EE1 中,尾部斜杠是在由
表达式引擎;在 EE2 中,不会生成尾部斜杠。例外情况
结构模块,它输出带有斜杠的URL
在 EE1 和 EE2 中

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:

<IfModule mod_rewrite.c>
    # Enable Apache's RewriteEngine
    RewriteEngine On

    # Add Trailing Slashes to URLs
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+[^/])$ /$1/ [R=301,L]

    # ExpressionEngine Remove index.php from URLs
    RewriteCond %{REQUEST_METHOD} !=POST
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule> 

Using the above code, example.com/mobile/index.php will not get rewritten to example.com/mobile/index.php/, nor will pages with anchors example.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 a www 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.

Sidenote: in EE1, trailing slashes are generated in URLs produced by
ExpressionEngine; in EE2, trailing slashes are not generated. The exception
is the Structure Module, which outputs URLs with trailing slashes
in both EE1 and EE2
.

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