【问题标题】:Change URL name with mod rewrite on htaccess在 htaccess 上使用 mod rewrite 更改 URL 名称
【发布时间】:2013-06-13 19:57:18
【问题描述】:

我很难理解 mod_rewrite 的工作原理。

我有一个这样的网址:

www.example.com/store/index.php?categ=headbands

我想让它看起来像这样:

www.example.com/store/headbands

我正在尝试这个,但不工作:

RewriteEngine on  
RewriteBase /store/
RewriteRule ^index/([^/\.]+)/?$ $1  [L]

我通过我找到的一个随机示例确认了 mod_rewrite 已激活。

有什么建议吗? 谢谢!

【问题讨论】:

  • 如果你想匹配一个领先的store,为什么你的第二条规则说index?为什么它会重写为$1 而不是index.php?categ=...
  • 我想既然 RewriteBase 已经把我带到了/store/ 位置,那么我需要指定下一个引用...
  • 不要将RewriteBase 用于如此简单的结构。它适用于更多奇怪的情况。

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


【解决方案1】:

试试这个

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^/store/(.*)$ /store/index.php?categ=$1 [NC,L]


使用以下代码将 .htaccess 文件放入“store”目录:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^(.*)$ index.php?categ=$1 [NC,L]

【讨论】:

  • 将 .htaccess 文件放在 store 目录中,并将最后一行代码替换为 RewriteRule ^(.*)$ index.php?categ=$1 [NL,C]。我希望这会奏效。
  • 哇,这真的没用:内部服务器错误。服务器遇到内部错误或配置错误,无法完成您的请求。
  • 哦!我的错误[NL,C] 导致该错误。 100%肯定现在它会被修复。将其替换为[NC,L]。祝你好运。
  • 访问此网址:addedbytes.com/articles/for-beginners/…。对你我这种url重写的初学者很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 2012-07-21
  • 1970-01-01
  • 1970-01-01
  • 2014-09-26
  • 1970-01-01
相关资源
最近更新 更多