.htaccess
是一个配置文件,主要用于 Apache Web 服务器,用于控制目录级别的访问权限和其他设置。通过 .htaccess
文件,可以实现 URL 重写(Rewriting),从而实现子域名伪静态。
subdomain.example.com/page
的形式。example.com/page
的形式。假设我们有一个子域名 blog.example.com
,我们希望将动态 URL blog.example.com/article.php?id=123
重写为 blog.example.com/article/123
。
.htaccess
文件:RewriteEngine On
RewriteBase /
# 重写子域名下的文章页面
RewriteCond %{HTTP_HOST} ^blog\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/article/.*$
RewriteRule ^article/([0-9]+)/?$ article.php?id=$1 [L,QSA]
确保 Apache 服务器启用了 mod_rewrite
模块,并且在主配置文件(通常是 httpd.conf
或 apache2.conf
)中允许使用 .htaccess
文件:
<Directory "/path/to/your/document/root">
AllowOverride All
</Directory>
.htaccess
文件位于正确的目录中。mod_rewrite
模块。RewriteCond
进行条件判断,避免不必要的重写。.htaccess
文件和目录有正确的权限。.htaccess
文件。通过以上配置和示例代码,你可以实现子域名伪静态,并解决常见的相关问题。
领取专属 10元无门槛券
手把手带您无忧上云