【问题标题】:htaccess rewrite rule variable is not supported不支持 htaccess 重写规则变量
【发布时间】:2015-02-25 15:23:38
【问题描述】:

我对 php 的重写规则有疑问,我希望它是:

localhost/myproject/XXXX => localhost/myproject/index.php?request=XXXX

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*)$ index.php?request=$1 [QSA,NC,L]
</IfModule>

我在 apache 上激活了 mod_rewrite,我的 index.php 在 WAMPDIR\www\MyProject 下

当我在这里测试时:http://htaccess.madewithlove.be/ 我明白了:

请帮忙,谢谢

【问题讨论】:

  • .htaccess 与 index.php (WAMPDIR\www\MyProject) 位于同一文件夹中,当我在浏览器上测试它或使用 Postman 时,我得到一个未找到的错误:The requested URL /myproject/xxx was not found on this server.
  • 显示500内部错误,所以文件没有问题,我认为问题来自规则。
  • localhost/myproject/index.php?request=XXXX 工作正常,但我想以这种方式访问​​它:localhost/myproject/XXXX 通过重写 url

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

/myproject/.htaccess中有此代码:

RewriteEngine On
RewriteBase /myproject/

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?request=$1 [QSA,L]

RewriteRule 之后不应有前导斜杠。

【讨论】:

  • 像魅力一样工作 :) 我用&lt;IfModule mod_rewrite.c&gt;&lt;/IfModule&gt; 包围了它,谢谢
【解决方案2】:

你真的不必把它弄得那么复杂。

这就足够了:

RewriteEngine On
RewriteBase /myproject/
RewriteRule ^/(.+) /myproject/index.php?request=$1 [L]

我强烈建议您阅读有关 htaccess 的更多信息。虽然看起来很像,但其实并没有那么复杂。

【讨论】:

  • 很抱歉它不起作用:The requested URL /incommmobilews/xxx was not found on this server.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-03-19
  • 1970-01-01
  • 2013-10-29
  • 1970-01-01
  • 1970-01-01
  • 2011-11-03
  • 1970-01-01
相关资源
最近更新 更多