【问题标题】:How to only rewrite specific urls without affecting other files如何只重写特定的url而不影响其他文件
【发布时间】:2018-03-06 10:50:11
【问题描述】:

我正在尝试重写网上商店的 url,因此页面没有文件扩展名,并且 url 在 url 中显示产品所属的类别。

我已经修复了类别部分,所以我的网址现在看起来像这样:

http://webshop.nl/douche/douche-2.html

但我希望它看起来像这样:

http://webshop.nl/douche/douche-2

我尝试了以下行:

RewriteRule         ^(.*)/(.*) product-page.php?cat=$1&alias=$2 [QSA,L]

但这会将所有文件重写为 product-page.php,包括 css/js/html 文件。我可以以某种方式排除这些或仅将该行应用于产品吗?

如果有帮助,这就是我通过alias 获取产品信息的方式:

$artikel = '
SELECT cnt.id, cnt.title as content_title, cnt.alias as content_alias, cnt.catid, cnt.images, cnt.state, cnt.introtext, cat.id, cat.title as cat_title, cat.alias as cat_alias,
MAX(case when f.field_id = 4 then f.value end) as prijs,
MAX(case when f.field_id = 5 then f.value end) as prijsoud,
MAX(case when f.field_id = 6 then f.value end) as specificaties
FROM snm_fields_values f
JOIN snm_content cnt
ON cnt.id = f.item_id
JOIN snm_categories cat
ON cnt.catid = cat.id
WHERE cnt.alias = "'.$conn->real_escape_string($_GET['alias']).'"
AND cnt.state = 1
GROUP BY f.item_id';
$artikelcon = $conn->query($artikel);
$artikel = $artikelcon->fetch_assoc();

【问题讨论】:

    标签: php .htaccess url-rewriting


    【解决方案1】:

    你可以使用这条规则:

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule ^([\w-]+)/([\w-]+)/?$ product-page.php?cat=$1&alias=$2 [QSA,L]
    
    • RewriteCond 确保只为非文件和非目录重写它
    • 最好在正则表达式中使用[\w-]+ 而不是.*,以避免匹配带有点字符的网址。

    【讨论】:

    • 谢谢!这完成了工作。我可以在条件下编写多个规则吗?因为我希望我的类别列表页面也一样。 - 顺便说一句,我可以在 5 分钟内接受答案。
    • 是的,您也可以使用类似的模式来编写类别页面。
    猜你喜欢
    • 1970-01-01
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-15
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多