【问题标题】:Remove .PHP and keep query string [closed]删除 .PHP 并保留查询字符串 [关闭]
【发布时间】:2012-01-15 21:40:50
【问题描述】:

我的代码:

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

在网址中显示为:

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

也没有 www 重定向到 www。

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

有什么想法吗?

【问题讨论】:

  • 有什么想法吗?怎么了?
  • 我收到错误:404 未找到。在此服务器上找不到 URL /test。
  • 这个诊断信息应该在问题中。

标签: php .htaccess


【解决方案1】:

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

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

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

【讨论】:

  • 那么删除.php扩展名的部分是正确的?!
【解决方案2】:
RewriteRule ^(.*).php(.*) $1$2 [NC]

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

相反,您需要分两个阶段进行:

  1. x.php 上,要求浏览器以x 重新请求(使用[R=301])。
  2. x 重写为真实的 URL 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]

【讨论】:

  • 错误:Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。但它在地址栏中没有正确重定向到 php。
  • @David19801:“类似”。您将不得不继续调试它。 stackoverflow.com/questions/5574442/… 可能是相关的——第一条规则上的 NS 标志可能会有所帮助。
【解决方案3】:

你的重写规则正好相反:当用户请求 index.php 时,他们会被重定向到 index.

试试这样的方法:

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]

【讨论】:

  • 再次出错:Firefox 检测到服务器正在以永远不会完成的方式重定向对该地址的请求。
猜你喜欢
  • 2018-05-11
  • 2017-05-23
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
相关资源
最近更新 更多