首页 > 解决方案 > 使用 HTAaccess 更改网站链接

问题描述

是否可以使用 .htaccess 重写子域?

例子:

http://fashion.example.com 显示的内容

http://example.com/index.php?name=fashion

<IfModule mod_rewrite.c>
# Turn on the rewrite engine
RewriteEngine  on

RewriteRule %{REQUEST_URI} ^(.*).example.com/$ index.php?name=$1

</IfModule>

标签: apache.htaccessmod-rewrite

解决方案


假设您的子域指向与主域相同的目录,您可以使用如下内容:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^((?!www).+)\.example.com$
RewriteRule ^(.+)/?$  /index.php?name=%1 [L]

推荐阅读