0

I'm changing

mydomain.com/directory/?name=lorem&action=posts

to

mydomain.com/directory/lorem/posts

But I'm now trying to add one called "other"

so I would like to get

mydomain.com/directory/?name=lorem&action=posts&other=38291389

to

mydomain.com/directory/lorem/post/38291389

This is the .htaccess code I am using for the first example

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(\w+)(/(\w+))?$ /directory/?name=$1&action=$3

and here's the one I tried to make for the second example, but it doesn't work

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d 
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^(\w+)(/(\w+))?$ /directory/?name=$1&action=$3&other=$4

Could somebody help me get this to work?

4

1 回答 1

1

把事情简单化:

RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} -d [OR] 
RewriteCond %{SCRIPT_FILENAME} -f [OR]
RewriteRule .* - [L]

RewriteRule ^(\w+)/(\w+)/(\w+)/?$ /directory/?name=$1&action=$2&other=$3
RewriteRule ^(\w+)/(\w+)/?$ /directory/?name=$1&action=$2
RewriteRule ^(\w+)/?$ /directory/?name=$1

在单个正则表达式中组合多个效果并没有真正的好处(虽然它很聪明并且让你感觉很好)但是当正则表达式引擎完成它的工作时,聪明的正则表达式可能需要更多的时间来匹配 2 或 3 个更简单的规则(如以上)。

于 2012-05-30T19:08:23.153 回答