1

我正在尝试在我的博客门户上使用 htaccess 实现以下目标:

结构 1

example.domain.com/link/sublink -> /blogdisplay.php?page=example&page2=link&page3=sublink

结构 2

en.domain.com/link/sublink/subsublink -> /index.php?lang=en&page=link&page2=sublink&page3=subsublink

结构 3

domain.com/link/sublink/subsublink -> /index.php?page=link&page2=sublink&page3=subsublink

我如何实现这一目标?这是我当前的代码(它现在根本不处理语言,但其他两个 URL 结构工作正常):

RewriteEngine On

RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} !^www.[NC]
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9-]{3,})\.domain.com$ [NC]
RewriteRule ^$ blogdisplay.php?page=%1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1 [L,QSA]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ blogdisplay.php?page=%1&page2=$1&page3=$2 [L,QSA]

## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f
## don't do anything
RewriteRule ^ - [L]

RewriteRule ^([a-z0-9-]+)/?$ index.php?page=$1 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2 [L,NC]
RewriteRule ^([a-z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/?$ index.php?page=$1&page2=$2&page3=$3 [L,NC]
4

1 回答 1

2

对于语言案例,您可以添加此附加案例(就在 R=301 规则下方):

RewriteCond %{HTTP_HOST} ^([a-z]{2})\.domain\.com$ [NC]
RewriteRule ^([a-z0-9-]+)/([a-z0-9-]+)/([a-z0-9-]+)/?$ index.php?lang=%1&page=$1&page2=$2&page3=$3 [L,NC,QSA]

PS:我也建议将规则移到If the request is for a valid file语言规则之上。

于 2013-09-28T16:59:29.447 回答