【问题标题】:How to do a basic URL rewrite with mod_rewrite如何使用 mod_rewrite 进行基本的 URL 重写
【发布时间】:2016-07-15 18:58:58
【问题描述】:

我不明白为什么,但是当我使用它时:

#RewriteRule ^/?r/(.*)$ /index.php?n=$1 [L]

mysite.com/r/somewhat 重写为mysite.com/index.php?r=somewhat 现场工作。

但如果我使用这个:

#RewriteRule ^/?(.*)$ /index.php?name=$1 [L]

mysite.com/somewhat 重写为mysite.com/index.php?r=somewhat, 我的网站停止工作。

我不明白为什么。有人可以帮助我吗?

如何将mysite.com/somewhat 重写为mysite.com/index.php?r=somewhat

【问题讨论】:

  • 如果你想要?r=somewhat,那你为什么要重写?n=$1nr 不同。

标签: php html apache .htaccess web


【解决方案1】:

您的第二条规则将导致无限循环,因为目标 URI /index.php?r=somewhat 也匹配 .*。最终导致 500 内部服务器错误。

要解决此问题,您需要避免使用 RewriteCond 重写文件和目录,如下所示:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?(.+)$ /index.php?r=$1 [L,QSA]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 2011-06-07
    • 2012-03-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    相关资源
    最近更新 更多