我想做一个重定向,以便为我的用户提供一个更清晰的 url。
我想改变:
http://mydomain.com/main/username/profile
至:
http://mydomain.com/username/profile
我会用重写或外星人来做这件事吗?怎么做?
我想做一个重定向,以便为我的用户提供一个更清晰的 url。
我想改变:
http://mydomain.com/main/username/profile
至:
http://mydomain.com/username/profile
我会用重写或外星人来做这件事吗?怎么做?
首先,您必须了解重定向和别名之间的区别。
重定向将发送请求/main/username/profile
到的用户/username/profile
。URL 将在浏览器中更改。如果搜索引擎可以访问 URL,这一点尤其重要,因为否则它们会将同一页面索引两次(重复内容)。
如果您决定使用重定向,您应该确定您的 URL 保持不变。原因是Cool URIs don't change。
重定向示例:
server {
# ...
location / {
# ...
location ~ /main/([a-zA-Z0-9]+)/profile$ {
# SEO effective redirect
return 301 /$1/profile;
}
# ...
}
}
别名用于告诉 nginx 请求的文件不是由文件系统上的 URL 映射的,它应该在其他地方查看。以下示例来自 nginx wiki:
root /var/www;
location /i/ {
alias /spool/w3/images/;
}
的请求/i/empty.gif
不会映射到/var/www/i/empty.gif
. 相反,它将匹配到/spool/w3/images/empty.gif
.