URL 重写 - 不带文件扩展名的重定向
我目前在 .htaccess 文件中使用以下规则:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
这可以很好地确保 /myfile.php
与 myfile
一样有效(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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用此规则重定向对
.php
文件的请求:Try this rule to redirect requests for
.php
files:您可以更改最后一个 RewriteRule 来执行此操作吗?
这会将“myfile”重定向到“myfile.php”。
此外,您还可以使用规则在 Google Analytics 内部对此进行调整。 尽管这可能不是一个理想的解决方案。
预计到达时间:如果您希望 myfile.php 重定向到 myfile,请尝试执行最后一条规则:
Could you just change your last RewriteRule to do this?
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:
您只需在网页中添加
https://support.google.com/webmasters /answer/139066?hl=zh-CN
You could just put in your pages a
https://support.google.com/webmasters/answer/139066?hl=en
我花了几个小时......但我相信我终于找到了答案。
在这里找到:http://www.webmasterworld.com/apache/3961636.htm
Took me a few hours...but I believe I have finally found the answer.
Found here: http://www.webmasterworld.com/apache/3961636.htm