URL 重写 - 不带文件扩展名的重定向

发布于 2024-07-16 05:39:28 字数 513 浏览 10 评论 0原文

我目前在 .htaccess 文件中使用以下规则:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

这可以很好地确保 /myfile.phpmyfile 一样有效(URL 中没有扩展名) )。 它还可以毫无问题地处理查询字符串,因此 myfile?var=foo 也可以工作。

问题是它们在 Google Analytics 中注册为两个单独的文件。 因此,虽然 myfile.php 可能是我的网站上第三个最受欢迎的页面(访问量为 X),但 myfile 可能是我的网站上第五个最受欢迎的页面(访问次数为 Y)。

我怎样才能进行硬重定向,而不是“接受任一”类型的规则?

I am currently using the following rules in a .htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

This works well to ensure that /myfile.php works as well as just myfile (with no extension in the URL). It also handles querystrings with no problems, so myfile?var=foo also works.

The problem is that these are registering in Google Analytics as being two separate files. So while myfile.php might be the third most popular page on my site, with X visits, myfile might be the fifth most popular page on my site with Y visits.

How can I do a hard redirect, rather than "accept either one" type of rule?

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

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

发布评论

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

评论(4

提赋 2024-07-23 05:39:28

尝试使用此规则重定向对 .php 文件的请求:

RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php
RewriteRule ^.+\.php$ /%1 [L,R=301]

Try this rule to redirect requests for .php files:

RewriteCond %{THE_REQUEST} ^GET\ /(([^/?]*/)*[^/?]+)\.php
RewriteRule ^.+\.php$ /%1 [L,R=301]
嘿看小鸭子会跑 2024-07-23 05:39:28

您可以更改最后一个 RewriteRule 来执行此操作吗?

RewriteRule ^(.*)$ $1.php [L,R=301]

这会将“myfile”重定向到“myfile.php”。

此外,您还可以使用规则在 Google Analytics 内部对此进行调整。 尽管这可能不是一个理想的解决方案。

预计到达时间:如果您希望 myfile.php 重定向到 myfile,请尝试执行最后一条规则:

RewriteRule ^(.+)\.php$ $1 [L,R=301]

Could you just change your last RewriteRule to do this?

RewriteRule ^(.*)$ $1.php [L,R=301]

That will redirect "myfile" to "myfile.php".

Also, you can adjust for this inside of Google Analytics using rules as well. Although that probably isn't an ideal solution.

ETA: If you want myfile.php to redirect to myfile, try this for your last rule instead:

RewriteRule ^(.+)\.php$ $1 [L,R=301]
风透绣罗衣 2024-07-23 05:39:28

您只需在网页中添加

<link rel="canonical" href="..." />

https://support.google.com/webmasters /answer/139066?hl=zh-CN

You could just put in your pages a

<link rel="canonical" href="..." />

https://support.google.com/webmasters/answer/139066?hl=en

榕城若虚 2024-07-23 05:39:28

我花了几个小时......但我相信我终于找到了答案。

// The Original
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

// Add This
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]

在这里找到:http://www.webmasterworld.com/apache/3961636.htm

Took me a few hours...but I believe I have finally found the answer.

// The Original
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

// Add This
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]

Found here: http://www.webmasterworld.com/apache/3961636.htm

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