.htaccess - 如何在 htaccess 和 mod-rewrite 上重写两个 GET 值
问题描述
我只想制作我发送的 GET 信息:
http://mywebsite.com/folder/index.php?category=category-alias-here http://mywebsite.com/folder/index.php?category=category-alias-here&page=5
在 URL 栏上重写为:
http://mywebsite.com/folder/category-alias-here/
http://mywebsite.com/folder/category-alias-here/5/
我不能。它很简单,我看到了几个类似的问题。在 stackoverflow 上测试了几个示例,它们都给了我 404 类别和两个页面。
这是我测试的代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?category=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?category=$1&page=$2 [L]
我知道这段代码对我的结构来说是错误的,我尝试了一些更改放置和删除 ([^/]+) 或将拳头 [L] 更改为 [N]。还有其他几件事。但没有运气。
请注意,整个网站都在一个真实的目录(/文件夹/)中,并且 .htaccess 也在那里(它不在 root 上),一旦我启动网站,一切都会在 public_html 上,但我不认为这是原因这个问题的。
是的,mod_rewrite 已开启。我实际上使用了一个 .htaccess,它可以在我放置的任何地方工作,并且它可以正确获取类别,但它有点混乱。
解决方案
找到了解决方案:
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([a-z-]+)\/([0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]
RewriteRule ^([a-z0-9-]+)\/$ index.php?category=$1&page=$2 [NC,L]
推荐阅读
- css - 如何将 bootstrap-vue 主题颜色设置为 css 变量?
- python - Python Pandas:嵌套字典
- python - sqlite3 fetchone() 在 Windows 上返回 sqlite3.Row 类型,但在 Mac 上返回元组
- python - Python:在 pdf 文件目录上运行打印文件应用程序时出现 pywintypes.error
- vue.js - 带有 VSCode 的 Vue.js 项目上的 HTML 和 CSS 注释标签错误
- c# - 在 UserControl 中使用 MainWindow 函数的正确方法是什么
- javascript - Chart.js 缩放 x 值
- php - laravel 内爆映射集合与链接(一个标签)
- angular - 如何在 Microsoft AppInsights 的 Angular TypeScript 中添加自定义角色
- svg - SVG 水平线太大