2011-12-08 43 views
1

我的代码:删除.PHP并保留查询字符串

Options +FollowSymlinks 
RewriteEngine on 
RewriteCond %{HTTP_HOST} ^example\.com$ [NC] 
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] 
RewriteRule ^(.*).php(.*) $1$2 [NC] 

我想使用它,以便

www.example.com/test.php?a=not&b=23 

显示在URL为:

www.example.com/test?a=not&b=23 

,也没有www重定向到www。

我的错误是: 404 not found. The URL /test not found on this server

任何想法?

+0

任何想法是什么?怎么了? –

+0

我收到错误:404找不到。未在此服务器上找到URL /测试。 – David19801

+0

这个诊断信息应该在问题中。 –

回答

2

您需要的标志QSA添加到您的RewriteRule指令:

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

QSA代表 “查询字符串追加”。

+1

那么删除.php扩展名的部分是正确的?! – David19801

0
RewriteRule ^(.*).php(.*) $1$2 [NC] 

你有点倒退了。您的规则采用URL x.php并将其重写为x。但是x不存在。

相反,你需要做的这两个阶段:

  1. x.php,要求浏览器重新请求刚刚x(使用[R=301])。
  2. x重写为真实网址x.php(而不是像你现在这样,而是用[QSA]而不是自己匹配查询字符串)。

所以,也许使用类似:

# Force browser to use condensed URL form 
# (and matches will not fall through to the next rule) 
RewriteRule ^(.*)\.php$ http://www.example.com/$1 [R=301,QSA,L,NC] 

# Make condensed URL form work 
RewriteRule ^(.*)$ $1.php [QSA] 
+0

错误:Firefox检测到服务器以永不完成的方式重定向该地址的请求。但它重定向到没有在地址栏中正确的PHP。 – David19801

+0

@ David19801:“类似的东西”。你将不得不继续调试它。 http://stackoverflow.com/questions/5574442/why-does-this-cause-an-infinite-request-loop可能是相关的 - 第一条规则的“NS”标志可能有所帮助。 –

0

你的RewriteRules做完全相反:当用户请求的index.php,他们会重定向到索引。

尝试这样的事情,而不是:

RewriteEngine On 
Options +FollowSymLinks 

# for the non-www to www 
RewriteRule %{HTTP_HOST} !^www\.example\.com$ [NC] 
RewriteRule^http://www.example.com/ 

# for the non-php to php (if no other file extension supplied) 
RewriteCond %{REQUEST_URI} !-f 
RewriteCond %{REQUEST_URI} !-d 
RewriteCond %{REQUEST_URI} !\.[A-Z]{3}$ [NC] 
RewriteRule ^(.+)$ $1.php [R,L,QSA] 
+0

错误再次发生:Firefox检测到服务器正在以永不完整的方式重定向该地址的请求。 – David19801