Htaccess 正则表达式重定向问题
当我在 htaccess 文件中使用它时:
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/ [R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]
如果您在没有 www 的情况下访问 site.com,它将重定向到 www.site.com/.php 而不是 www.site.com。
有什么想法吗?
谢谢!
When I use this in my htaccess file:
RewriteCond %{HTTP_HOST} ^site\.com$ [NC]
RewriteRule ^(.*)$ http://www.site.com/ [R=301]
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.+)$ $1.php [NC,L]
If you go to site.com without www it redirects to www.site.com/.php instead of www.site.com.
Any thoughts?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
也许是旧
.htaccess
中的浏览器缓存?尝试清空缓存,或使用其他浏览器(或者可能是Priavte Browsing)。Maybe it's browser's cache from your old
.htaccess
? Try to empty the cache, or use other browser (or maybe Priavte Browsing).我认为根 SCRIPT_FILENAME 将是“/”,它不会被视为有效文件,并且会触发 !-f 导致应用规则。
由于 / 是正则表达式捕获的字符,因此它会将其重写为 www.site.com/.php
I think at the root SCRIPT_FILENAME would be "/", which would not be considered a valid file and trip the !-f causing the rule to be applied.
Since the / is a character caught by the regex it will rewrite it to www.site.com/.php
尝试通过添加“,L”来将“last”添加到规则中:
Try to add "last" to rule by adding ",L":
我最终通过将 $1 添加到 RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
感谢您的帮助。
I ended up fixing it by adding $1 to RewriteRule ^(.*)$ http://www.site.com/$1 [R=301,L]
Thanks for all your help.