【问题标题】:Mod_Rewrite not rewriting URLsMod_Rewrite 不重写 URL
【发布时间】:2015-07-11 08:32:25
【问题描述】:

我有以下 url 结构:

  • /page.php?file=about
  • /page.php?file=contact
  • 等

.htaccess 文件的代码如下:

RewriteEngine On
RewriteRule ^([^/]*)\.html$ /page.php?file=$1 [L]

根本不尝试重写 URL。我正在寻找类似 /about.html、/contact.html

的内容

是.htaccess 代码还是服务器上的某种设置?

【问题讨论】:

  • 只是一些额外的信息,我之前在同一台服务器上安装了 Wordpress,并且可以很好地重写 URL,所以我很确定这是我做错了!
  • 您在浏览器中输入的网址是什么? WP 还在用吗?
  • Wordpress 现已关闭,这只是我创建并尝试使用大型基本模板系统的一个小站点。我输入的网址是 site.com/page.php?file=about

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

你可以使用:

RewriteEngine On

RewriteCond %{THE_REQUEST} /page\.php\?file=([^\s&]+) [NC]
RewriteRule ^ /%1.html? [R=302,L,NE]

RewriteRule ^([^/]+)\.html$ page.php?file=$1 [L,QSA,NC]

【讨论】:

  • 这会重写 URL,但实际上尝试查找 about.html,然后显然是 404,因为没有这样的文件。
  • 肯定还是有问题,因为它不是那样工作的
  • 抱歉,我不确定需要什么信息。如果我删除 .htaccess - 站点会按预期呈现带有关于页面内容的页面模板 (page.php)。我只是想让该 URL 看起来更像 /about.html 而不是 page.php?file=about
  • 未找到 在此服务器上未找到请求的 URL /about.html。
  • 您的设置不正确。 public_html dir 应该是网站根目录,但这里是一个子目录。我相信page.php 也在public_html 中,对吗?如果您控制 Apache,那么最好将 public_html 设为您的 DocumentRoot
猜你喜欢
  • 1970-01-01
  • 2011-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-10
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多