You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Apache的.htaccess中将旧URL 'more'重定向到'details'?

Apache下将旧URL /more 重定向到 /details 的.htaccess配置方案

问题背景

需要在Apache环境中,通过.htaccess配置把旧URL /more(包括无扩展名和带.htm后缀的情况)重定向到新URL /details。目前靠文件系统符号链接临时解决,但希望用配置文件实现。之前试了多种mod_rewrite正则和mod_alias,要么出现500错误,要么重定向不生效导致404。网站已经启用无扩展名URL规则,比如访问/welcome会自动重写为/welcome.htm。

当前网站.htaccess内容

##
## Adjusted for the website’s production phase, publicly visible
## ==============================================================
##

RewriteEngine On

# disable directory browsing
Options All -Indexes

## ###################
## Forbid access to .htaccess by browser, now produces a 403 Forbidden error; n.b. useful for testing 403)
##
<FilesMatch "\.?htaccess$">
    order allow,deny
    deny from all
</FilesMatch>

Header add Content-Security-Policy "default-src 'self';"
Header set Content-Language en-GB

##
## Extensionless URLs technology; if requested no-ext file does not exist,
## and is not a directory and is not already .htm, then rewrite no-ext to .htm
##
##RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.htm$
RewriteRule ^(.*)$ $1.htm

AddDefaultCharset utf-8
AddCharset utf-8 .css .js .xml .xhtml .html .xhtm .htm .txt
AddLanguage en-GB .htm .xhtml .html .xhtm .txt

AddType text/css                .css
AddType application/javascript  .js
AddType application/xhtml+xml   .xhtml .html .xhtm .htm
AddType application/xml         .xml
AddType application/manifest+json  .webmanifest .manifest .sitemanifest
AddType text/plain      .txt
AddType image/svg+xml   .svg
AddType image/jpeg      .jpg .jpeg
AddType image/png       .png
AddType image/webp      .webp
AddType image/x-icon    .ico
AddType font/ttf        .ttf

ExpiresActive On
ExpiresDefault                      A3600
ExpiresByType image/jpeg            A2592000
ExpiresByType image/png             A2592000
ExpiresByType image/apng            A2592000
ExpiresByType image/webp            A2592000
ExpiresByType image/svg+xml         A2592000
ExpiresByType image/x-icon          A2592000
ExpiresByType font/ttf              A2592000
ExpiresByType application/manifest+json     A2592000
ExpiresByType application/javascript        A2592000
##
## Shorter suggested cache lifetime for file types more likely to change, in case we should do edits
##
ExpiresByType text/css                  A432000
ExpiresByType application/xhtml+xml     A432000
ExpiresByType text/plain                A1200
ExpiresByType application/xml           A1200


####################
# GZIP COMPRESSION #
####################
SetOutputFilter DEFLATE
AddOutputFilterByType DEFLATE text/html text/css text/plain text/xml image/svg+xml application/xhtml+xml application/xml application/javascript application/x-javascript application/x-httpd-php font/ttf application/manifest+json
BrowserMatch ^Mozilla/4         gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678]     no-gzip
BrowserMatch \bMSIE             !no-gzip !gzip-only-text/html
BrowserMatch \bMSI[E]           !no-gzip !gzip-only-text/html
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|webp|ttf)$ no-gzip

### declare the error documents’ names
ErrorDocument 404 "/404"
ErrorDocument 403 "/403"

#########
### Declare the start page
#########
# Uses a level of indirection via "_" in order to avoid having to mention the start page name twice

RewriteRule ^/$     _
RewriteRule ^$      _
RewriteRule ^_$     welcome

解决方案

核心规则添加

在.htaccess里的RewriteEngine On之后、无扩展名URL重写规则之前,添加以下重定向规则:

# 永久重定向 /more 和 /more.htm 到 /details
RewriteRule ^more(\.htm)?$ /details [R=301,L]

规则说明

  • ^more(\.htm)?$:精准匹配请求路径为/more或/more.htm的情况,(\.htm)?表示.htm后缀是可选的。
  • [R=301,L]:R=301是永久重定向,能让搜索引擎更新索引;L标记表示这是最后一条匹配规则,防止后续规则继续处理当前请求。

修改后的关键配置片段

RewriteEngine On

# 新增重定向规则
RewriteRule ^more(\.htm)?$ /details [R=301,L]

# disable directory browsing
Options All -Indexes

# ... 其余原有配置保持不变 ...

## Extensionless URLs technology; if requested no-ext file does not exist,
## and is not a directory and is not already .htm, then rewrite no-ext to .htm
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.htm$
RewriteRule ^(.*)$ $1.htm

注意事项

  1. 规则顺序不能乱:重定向规则必须放在无扩展名重写规则前面,否则请求会先被转成/more.htm,导致重定向规则匹配不到。
  2. 清除缓存再测试:301重定向会被浏览器缓存,测试时建议用隐私模式或者手动清除缓存。
  3. 确认mod_rewrite已启用:确保Apache服务器已经开启mod_rewrite模块,否则所有重写规则都不会生效。

内容的提问来源于stack exchange,提问作者Cecil Ward

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 08:27:33