Apache htaccess条件重写

Apache htaccess条件重写,apache,.htaccess,Apache,.htaccess,我在摆弄.htaccess和mod_重写。我有一个网站有两种类型的URL,我想重写: /index.php?nav=$2 /index.php?nav=41&intNewsId=$3--41是静态的,新闻导航总是41 我想将它们改写为: /pagename/id /news/pagename/id 我已经编写了一段代码,但是如果我添加最后一行,第二行就会停止工作,我可以想象这是因为第三个块中的条件对于第二个块也是如此。但我不知道如何正确使用条件。(两个模块单独工作) 我尝试了我能想到的一

我在摆弄.htaccess和mod_重写。我有一个网站有两种类型的URL,我想重写:

  • /index.php?nav=$2
  • /index.php?nav=41&intNewsId=$3
    --41是静态的,新闻导航总是41
我想将它们改写为:

  • /pagename/id
  • /news/pagename/id
我已经编写了一段代码,但是如果我添加最后一行,第二行就会停止工作,我可以想象这是因为第三个块中的条件对于第二个块也是如此。但我不知道如何正确使用条件。(两个模块单独工作)

我尝试了我能想到的一切,但我对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
谢谢大家的回答

试试这个:

# 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
RewriteRule ^news/.+/([^/]*)$ /index.php?nav=41&intNewsId=$1 [L]
RewriteRule ^.+/([^/]*)$ /index.php?nav=$1 [L]
试试这个:

# 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
RewriteRule ^news/.+/([^/]*)$ /index.php?nav=41&intNewsId=$1 [L]
RewriteRule ^.+/([^/]*)$ /index.php?nav=$1 [L]

另一种方法可能更简洁一些:

RewriteRule ^pagename/(\d+)$ index.php?nav=$1
RewriteRule ^news/pagename/(\d+)$ index.php?nav=41&intNewsId=$1

为什么这个话题被否决了?另外,请注意,您可以通过使用捕获一个或多个数字的任何块的
(\d+)
,来避免那些
[0-9]
构造的笨拙。谢谢您。注意我的([0-9]|[1-9][0-9]|[1-9][0-9][0-9]),以前情况更糟:汉克斯先生,用这个它工作了(我把pagename改成了一个多字符正则表达式检查)我也修复了我自己的代码工作顺便说一句,我只是去掉了第二个$1-9][0-9][0-9]),这样情况就不会干扰记录的最后一个,可以使用适当的
RewriteRule
s:)省略
RewriteCond
语句@Hans Wassink很高兴它起作用了。根据条件编写规则总是很好的,这样会使它更有效。谢谢你。用这个我也让它工作了:D多好的一天学习^^^@Hans Wassink:不客气!如果我的答案解决了你的问题,请考虑通过点击在我的答案左侧的投票数以下的记号来接受它。