不同服务器软件配置方式差异显著,需根据实际环境选择对应方案:
步骤1:启用mod_rewrite
模块
在httpd.conf
或apache2.conf
中取消注释LoadModule rewrite_module modules/mod_rewrite.so
,确保模块加载。
步骤2:配置.htaccess
文件
在网站根目录创建或修改.htaccess
,添加以下规则实现URL重写:
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
此规则将动态请求映射为静态URL格式(如/article/123
转为index.php?url=article/123
)。
步骤3:设置目录权限
确认Apache配置中AllowOverride All
已启用,允许.htaccess
覆盖全局规则。
步骤1:修改站点配置文件
在/etc/nginx/sites-available/your_site
的server
块内添加重写逻辑:
location / { try_files $uri $uri/ /index.php?url=$uri&$args; }
此配置优先匹配真实文件,未命中则转发至入口脚本。
步骤2:检查正则匹配(高级需求)
使用rewrite
指令处理复杂路由:
rewrite ^/blog/(d+)/?$ /blog.php?id=$1 last;
将/blog/123
映射到blog.php?id=123
。
步骤3:重载配置
执行nginx -s reload
使新配置生效。
步骤1:安装URL重写模块
通过Microsoft Web Platform Installer安装「URL Rewrite」扩展。
步骤2:配置web.config
文件
在网站根目录的web.config
中添加规则:
<rule name="Rewrite to index.php"> <match url="^(.*)$" /> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="index.php?url={R:1}" /> </rule>
技术配置完成后,需从搜索引擎友好性角度优化URL结构:
静态化处理
百度明确建议使用静态或伪静态URL(如/product/123.html
),避免过长参数(如?id=123&category=5
),可通过服务器重写实现伪静态,提升抓取效率。
规范化(Canonicalization) 若存在多个URL(如带www
与不带),需在<head>
中添加:
<link rel="canonical" href="https://example.com/standard-url" />
或在服务器配置中301重定向至首选版本。
层级扁平化
控制URL深度,不超过3级(如/news/2025/seo-guide
优于/category/12/news/2025/08/guide
),便于蜘蛛抓取。
关键词嵌入
在URL路径中包含目标关键词(英文或拼音),例如/seo-jiaocheng
而非/page123
,增强相关性信号。
屏蔽无效参数
使用百度站长平台的“URL参数”工具,标记不影响内容的参数(如utm_source
),避免重复内容问题。
百度优先索引HTTPS页面,需确保全站加密:
强制HTTPS
RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
if ($scheme != "https") { return 301 https://$host$request_uri; }
HSTS预加载
在响应头中添加Strict-Transport-Security: max-age=31536000; includeSubDomains; preload
,减少重定向开销。
A→B→C
应简化为A→C
)。