URL重写-没有文件扩展名的重定向 [英] URL Rewriting - Redirect With No File Extension
问题描述
我当前在.htaccess文件中使用以下规则:
RewriteEngine onRewriteCond%{REQUEST_FILENAME}!-dRewriteCond%{REQUEST_FILENAME} \.php -fRewriteRule ^(.*)$ $ 1.php
这很好地确保了/myfile.php
和 myfile
一样工作(URL中没有扩展名).它还可以毫无问题地处理查询字符串,因此 myfile?var = foo
也可以使用.
问题是这些文件在Google Analytics(分析)中注册为两个单独的文件.因此,虽然 myfile.php
可能是我的网站上访问量排名第三的第三页,而 myfile
却是我的网站上的用户访问次数排名第五的第五页./p>
我该如何执行硬重定向,而不是接受任一"规则?
您能更改上一个RewriteRule来做到这一点吗?
RewriteRule ^(.*)$ $ 1.php [L,R = 301]
这会将"myfile"重定向到"myfile.php".
此外,您也可以使用规则在Google Analytics(分析)中对此进行调整.尽管那可能不是理想的解决方案.
ETA :如果您希望myfile.php重定向到myfile,请尝试使用它作为您的最后一条规则:
RewriteRule ^(.+)\.php $ $ 1 [L,R = 301]
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?
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]
这篇关于URL重写-没有文件扩展名的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!