2

我正在尝试替换问号和等号,以便可以使用如下 URL:

http://www.mydomain.com/categories/id/23/name/category-name

上面的 url 将在内部重定向到 http://www.mydomain.com/categories?id=23&name=category-name

我正在使用这个.htaccess:

Options +FollowSymLinks -MultiViews
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301]

# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]

但它没有按预期工作。当我访问http://www.mydomain.com/categories/id/23/name/category-name时,我收到 500 内部服务器错误。为什么?我究竟做错了什么?

更新代码

Options +FollowSymLinks -MultiViews
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA] 
4

1 回答 1

2

这条规则应该负责递归地替换每一个/=并最终使它成为一个很好的查询字符串:

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]

您需要注释掉或删除这条规则:

# RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]

更新:你 .htaccess 需要重新排序和一点点调整。这应该有效:

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/(.+?)&?$ $1.php?$2 [L,QSA] 

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
于 2013-09-03T05:39:59.483 回答