我正在摆弄.htaccess和mod_rewrite。我有一个网站,有两种类型的URL我想重写:
/index.php?nav=$2
/index.php?nav=41&intNewsId=$3
- 41是静态的,新闻导航总是41 我想将它们重写为:
/pagename/id
/news/pagename/id
我已经制作了一段可行的代码,但如果我添加最后一行,则第二行停止工作,我可以想象这是因为第三个块中的条件对于第二个块也是如此。但我无法弄清楚如何正确使用条件。 (两个块都是个人工作)
Options +FollowSymlinks
RewriteEngine on
# Reroute rules that end on /
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$ /$1/$2/ [R]
# Make the system understand pagename/96
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=$2
# Make the system understand news/pagename/99
RewriteRule ^(.*)\/(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=41&intNewsId=$3
我尝试了所有我能想到的东西,但是我对htaccess中的这种正则表达式的输入或条件块并不太熟悉。
解决方案: 我修改了自己的代码,我只是剥掉了第二个$,所以条件没有干扰最后一个
Options +FollowSymlinks
RewriteEngine on
# Reroute rules that end on /
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])$ /$1/$2/ [R]
# Make the system understand pagename/96
RewriteRule ^(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/ /index.php?nav=$2
# Make the system understand news/pagename/99
RewriteRule ^(.*)\/(.*)\/([0-9]|[1-9][0-9]|[1-9][0-9][0-9])/$ /index.php?nav=41&intNewsId=$3
感谢所有答案!
答案 0 :(得分:1)
试试这个:
RewriteRule ^news/.+/([^/]*)$ /index.php?nav=41&intNewsId=$1 [L]
RewriteRule ^.+/([^/]*)$ /index.php?nav=$1 [L]
答案 1 :(得分:1)
另一种方法,可能稍微简洁一点:
RewriteRule ^pagename/(\d+)$ index.php?nav=$1
RewriteRule ^news/pagename/(\d+)$ index.php?nav=41&intNewsId=$1
答案 2 :(得分:0)
试试这个:
# Make the system understand pagename/96
RewriteCond %{REQUEST_URI} ^/pagename/([0-9]*)
RewriteRule .* /index.php?nav=%1
# Make the system understand news/pagename/99
RewriteCond %{REQUEST_URI} ^/news/pagename/([0-9]*)
RewriteRule .* /index.php?nav=41&intNewsId=%1