简体   繁体   English

当访问不带www的网站时,mod_rewrite添加.php扩展名。 字首

[英]mod_rewrite Adds .php Extension When Accessing Site Without www. Prefix

My .htaccess file is as follows: 我的.htaccess文件如下:

Options -Multiviews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

The issue that I am having is that when I try to access my site without the www. 我遇到的问题是,当我尝试访问没有www的网站时。 prefix, the .php extension is added to the address, which can often cause a problem. 前缀,将.php扩展名添加到地址中,这通常会导致问题。 For example, if I try to access my homepage with the address example.com, that address is transformed into www.example.com/.php. 例如,如果我尝试使用地址example.com访问我的主页,则该地址将转换为www.example.com/.php。 I want the www. 我想要www。 to be added, but the .php extension added at the end just causes an error. 要添加,但是末尾添加的.php扩展名只会导致错误。 How do I fix this? 我该如何解决?

I believe the problem is with your last line. 我相信问题出在您的最后一行。 Mod Rewrite does not stop after executing the first rule. 执行第一个规则后,Mod Rewrite不会停止。 Try adding the last rule modifier to the redirect rule: 尝试将最后一个规则修饰符添加到重定向规则中:

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

You need to tell apache to stop processing rules after the 301 redirect, do this by adding an L - eg: 您需要告诉apache在301重定向后停止处理规则,方法是添加L-例如:

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

aside: you'd get on better if you enabled multiviews on the first line, and removed the last 3 lines ;) 撇开:如果在第一行启用多视图,并删除最后三行,则效果会更好;)

从.htaccess中删除以下行,然后尝试。

RewriteRule ^(.*)$ $1.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM