【问题标题】:decide page based on number of parameters - htaccess url rewrite根据参数数量决定页面 - htaccess url rewrite
【发布时间】:2013-07-22 18:20:26
【问题描述】:

我正在 htaccess 上编写 RewriteRule(s),它根据参数的数量决定它重定向到哪个页面。

例如,“http://mysite.com/tommy” 这将重定向到“mysite.com/user.php?user=tommy”

然而,“http://mysite.com/tommy/pets” 将重定向到“mysite.com/show.php?user=tommy&category=pets”

注意,第一个 URL 只有一个参数,它重定向到 user.php,第二个 URL 有两个参数,而是重定向到 show.php。

请帮忙,提前致谢!

目前...

<IfModule mod_rewrite.c> 
RewriteEngine On
RewriteBase / 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-l 
RewriteRule ^([^/]*)/?(.*)$ category.php?userId=$1&category=$2 [QSA,L] 
RewriteRule ^(.*)$ user.php?userId=$1 [QSA,L] 
</IfModule>

【问题讨论】:

  • 不太清楚你在问什么。 “我在写……”你说。如果你已经写了,那有什么问题呢?如果你还没有让它如你所愿,展示你已经拥有的东西。

标签: .htaccess mod-rewrite url-rewriting


【解决方案1】:

完成了,给其他需要这个的人...

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?userId=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?userId=$1 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ category.php?userId=$1&category=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ category.php?userId=$1&category=$2 [L]

【讨论】:

    猜你喜欢
    • 2017-07-18
    • 2020-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多