【问题标题】:How to create clean url using .htaccess for multiple parameters?如何使用 .htaccess 为多个参数创建干净的 url?
【发布时间】:2015-11-01 17:03:41
【问题描述】:

我尝试为我的网站创建干净的 URL。 所需格式为:site/category/num example site/sport/3

category 和 page 都是可选参数。所以我有三个规则。问题是规则三。它只在第一次工作。

用户进入网站 - tule 1. OK。 用户导航和选择类别。规则 2... 好的。 用户导航并想要第 2 页。 (规则 3)好的。 用户希望在第 3 页中查看更多项目(规则 3)错误..

我得到参数 site/category/category/3 并且导航失败。现在我不明白为什么我会两次获得类别。

我的 .htaccess 文件几乎可以正常工作。

RewriteEngine On

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /index.php?page=$1 [L]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?category=$1 [L]

RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC]
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)$ index.php?category=$1&page=$2 [NC,L]

【问题讨论】:

    标签: apache .htaccess mod-rewrite configuration


    【解决方案1】:

    尝试:

    RewriteEngine On
    RewriteCond %{REQUEST_URI} (\.css|\.js|\.png|\.jpg|\.gif|robots\.txt)$ [NC,OR]
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    
    RewriteRule ^([0-9]+)/?$ /index.php?page=$1 [L]
    RewriteRule ^([^/]+)/?$ /index.php?category=$1 [L]
    RewriteRule ^([0-9a-zA-Z_-]+)/([0-9]+)/?$ /index.php?category=$1&page=$2 [NC,L]
    

    【讨论】:

    • 我仍然得到相同的双重类别。
    • 这不是htaccess的问题。您需要在您的 html 代码头中添加<base href="/">
    • 是的,我的链接已损坏... 好的已修复。现在服务器将 404 错误响应到 200 代码....对此有什么提示吗?
    猜你喜欢
    • 2015-02-25
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多