【问题标题】:Url rewriting using RewriteCond whrere different url required based on query param使用 RewriteCond 重写 URL,根据查询参数需要不同的 url
【发布时间】:2021-08-09 01:50:20
【问题描述】:
我的要求是
- domain.com/src/home.php?page=category&category=recipe
- domain.com/src/home.php?page=article&key=myarticle
- domain.com/src/home.php
预期的结束网址
-
domain.com/src/category/recipe
-
domain.com/src/article/myarticle
-
域名.com
我写了一个
RewriteRule ^src/([A-Za-z0-9-]+)/?$ src/home.php?page=article&key=$1 [NC,L]
问题是,如果一个工作正常,那么另一个不工作。
任何人都可以根据“页面”查询参数建议如何使其在某些条件下工作。
提前致谢。
【问题讨论】:
标签:
regex
.htaccess
mod-rewrite
【解决方案1】:
使用您显示的示例,尝试,请尝试遵循 htaccess 规则文件。确保将您的 htaccess 文件与 src 文件夹(不在其中)一起保存。
另外,请确保在测试 URL 之前清除浏览器缓存。
RewriteEngine ON
RewriteBase /src/
##Rules for handling url domain.com/src/category/recipe here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(category)/([\w-]+)/?$ src/home.php?page=$1&category=$2 [QSA,NC,L]
##Rules for handling urls like: domain.com/src/article/myarticle
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^src/(articile)/([\w-]+)/?$ src/home.php?page=$1&key=$2 [QSA,NC,L]
##Rule for handling domain.com url here.
RewriteRule ^/?$ src/home.php [QSA,L]