我的本地机器是阿帕奇这是重定向方案nginx. 如何使用 RewriteRules 来编写此内容?
location / {
if (!-e $request_filename){
rewrite ^/$ /index.php?url=home;
}
}
location /admin {
rewrite ^/admin$ /access.php;
rewrite ^/admin/(.*)$ /access.php?page=$1;
}
location /s {
rewrite ^/s/([^\/]+)(\/|)?$ index.php?url=share&share=$1;
}
location /media {
rewrite ^/media/([^\/]+)(\/|)?$ index.php?url=home&media=$1;
}
location /lang {
rewrite ^/lang/([^\/]+)(\/|)?$ index.php?url=lang&lang=$1;
}
location /page {
rewrite ^/page/([^\/]+)(\/|)?$ index.php?url=page&page=$1;
}
location /articles {
rewrite ^/articles(\/|)$ index.php?url=articles;
rewrite ^/articles/([^\/]+)(\/|)?$ index.php?url=articles&id=$1;
rewrite ^/articles/([^\/]+)/post/([^\/]+)(\/|)?$ index.php?url=articles&id=$1&post=$2;
}
答案1
你只需要激活mod_rewrite在您的 Apache 配置中并添加到您的虚拟主机配置:
RewriteEngine on
RewriteRule "^/$" "/index.php?url=home" [L]
RewriteRule "^/admin$" "/access.php" [L]
RewriteRule "^/admin/(.*)$" "/access.php?page=$1" [L]
RewriteRule "^/s/([^\/]+)(\/|)?$" "index.php?url=share&share=$1" [L]
RewriteRule "^/media/([^\/]+)(\/|)?$" "index.php?url=home&media=$1" [L]
RewriteRule "^/lang/([^\/]+)(\/|)?$" "index.php?url=lang&lang=$1" [L]
RewriteRule "^/page/([^\/]+)(\/|)?$" "index.php?url=page&page=$1" [L]
RewriteRule "^/articles(\/|)$" "index.php?url=articles" [L]
RewriteRule "^/articles/([^\/]+)(\/|)?$" "index.php?url=articles&id=$1" [L]
RewriteRule "^/articles/([^\/]+)/post/([^\/]+)(\/|)?$" "index.php?url=articles&id=$1&post=$2" [L]
该指令几乎与nginx指令并描述mod_rewrite 手册。