我正在尝试转换我的应用程序链接,以便像这样的链接:
http://localhost/index.php?id=13&category=Uncategorized&title=just-a-link
转换成这样:
http://localhost/13/Uncategorized/just-a-test
到目前为止,我能够使用:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
但这完全破坏了到 css 和其他文件的链接,因为它将每个带有查询的请求重定向到 index.php
所以我稍微改变了它,让它只在第一个查询是数字时运行:
RewriteEngine On
RewriteRule ^([0-9]*)/([^/]*)/([^/]*)$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
这不会破坏 css 和 js 文件,但是当您转到链接时,例如 http://localhost/13/cat/test
然后尝试转到另一个链接,例如 http://localhost/19/Uncategorized/something
在上一页中点击它,它会将您重定向到类似 http://localhost/13 的地方/Uncategorized/19/Uncategorized/只是一个测试
如何在没有任何这些奇怪的副作用的情况下实现我所描述的目标?
最佳答案
使用:
RewriteEngine On
RewriteRule ^([0-9]+)/([^/.]+)/([^/.]+)/?$ /index.php?id=$1&category=$2&title=$3 [QSA,L]
并将此代码添加到您的 html <header>
:
<base href="/">
或
<base href="http://www.domain.com/">
修复相关的 css 和 js 链接
关于php - mod_rewrite 问题和 php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32335473/