htacces url-rewrite for multiple variables

时间:2018-04-18 16:12:09

标签: .htaccess

我在htaccess中有以下网址重写:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/?([0-9a-zA-Zßäöü\s]+)?/?([0-9a-zA-Z.ßäöü\s]+)?/?([0-9a-zA-Z.ßäöü\s]+)?/?([0-9]+)? category.php?mid=$1&cid=$2&ccid=$3&cccid=$4&ccccid=$5&page=$6 [NC,L]

category / $ 1 / $ 2应该打开catagory.php?mid = $ 1& cid = $ 2(并以page = 1开头);这适用于上面的代码。

但是,我希望以下网址也能正常运行“ category / $ 1 / $ 2 / $ 6应该打开category.php?mid = $ 1& cid = $ 2& page = $ 6.

请注意,$ 6总是一个数字,类别$ 2,$ 3,$ 4和$ 5是数字和字母的组合。

示例:

  • category/19/sport (mid=19, cid=sport)

  • category/19/sport/2 (mid=19, cid=sport, page=2)

  • category/19/sport/soccer/2.championsleague/2016results (mid=19, cid=sport, ccid=soccer, cccid=2.championsleague, ccccid=2016results)

  • category/19/sport/soccer/2.championsleague/2016results/10 (mid=19, cid=sport, ccid=soccer, cccid=2.championsleague, ccccid=2016results, page=10)

我应该如何更改网址重写?

1 个答案:

答案 0 :(得分:1)

您可以在您的网站根目录中使用这些规则.htaccess:

Options -MultiViews
RewriteEngine On

# skip all rules below for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^category(\d+)/([^/]+)/?$ category.php?mid=$1&cid=$2 [L,NC,QSA]

RewriteRule ^category(\d+)/([^/]+)/(\d+)/?$ category.php?mid=$1&cid=$2&page=$3 [L,NC,QSA]

RewriteRule ^category(\d+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ category.php?mid=$1&cid=$2&ccid=$3&ccccid=$4&cccccid=$5 [L,NC,QSA]

RewriteRule ^category(\d+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/(\d+)/?$ category.php?mid=$1&cid=$2&ccid=$3&ccccid=$4&cccccid=$5&page=$6 [L,NC,QSA]