大网数据广告活动
网站运营与故障问答

Apache doesn't redirect subfolders

· 52CE 我爱测

I'm having trouble configuring my Apache server through the .htaccess file. I want to redirect all request from my domain ( example.com , www.example.com , example.com/folder1/folder2 ) to my new domain ( new-example.com , www.ne…

问题原文

原帖链接 · Jose A. Matarán · CC BY-SA 4.0

I'm having trouble configuring my Apache server through the .htaccess file. I want to redirect all request from my domain (example.com, www.example.com, example.com/folder1/folder2) to my new domain (new-example.com, www.new-example.com, new-example.com/folder1/folder2).

My .htaccess looks like:

# BEGIN WordPress
# Las directivas (líneas) entre «BEGIN WordPress» y «END WordPress» son
# generadas dinámicamente y solo deberían ser modificadas mediante filtros de WordPress.
# Cualquier cambio en las directivas que hay entre esos marcadores serán sobrescritas.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteRule ^android-chrome-192x192\.png /wp-content/uploads/fbrfg/android-chrome-192x192.png [QSA,L]
RewriteRule ^android-chrome-512x512\.png /wp-content/uploads/fbrfg/android-chrome-512x512.png [QSA,L]
RewriteRule ^apple-touch-icon\.png /wp-content/uploads/fbrfg/apple-touch-icon.png [QSA,L]
RewriteRule ^browserconfig\.xml /wp-content/uploads/fbrfg/browserconfig.xml [QSA,L]
RewriteRule ^favicon-16x16\.png /wp-content/uploads/fbrfg/favicon-16x16.png [QSA,L]
RewriteRule ^favicon-32x32\.png /wp-content/uploads/fbrfg/favicon-32x32.png [QSA,L]
RewriteRule ^favicon\.ico /wp-content/uploads/fbrfg/favicon.ico [QSA,L]
RewriteRule ^mstile-150x150\.png /wp-content/uploads/fbrfg/mstile-150x150.png [QSA,L]
RewriteRule ^safari-pinned-tab\.svg /wp-content/uploads/fbrfg/safari-pinned-tab.svg [QSA,L]
RewriteRule ^site\.webmanifest /wp-content/uploads/fbrfg/site.webmanifest [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

# My changes
DirectorySlash Off
Options +FollowSymLinks
Options -MultiViews
RewriteEngine on
RewriteBase /
RewriteOptions Inherit
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)/(.*)$ https://new-example.com/$1 [R=301,L]

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php72” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php72 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit

When I access www.example.com everything works fine, I am redirected to new-example.com but when I access a subfolder inside the old domain (e.g. www.example.com/subfolder1/subfolder2) the redirect doesn't work.

Can anyone help me to find the mistake?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

You've put your "redirect" directives in the wrong place - they need to go at the top of the file, before the # BEGIN WordPress section.

By placing them later in the file (after the WP front-controller) then they are only going to get processed for requests that map directly to physical files and directories (which includes the document root). So, in this case, I suspect subfolder is just a virtual path segment, not a physical subdirectory.


Aside:

I would also question why you've set DirectorySlash Off and included RewriteOptions Inherit?

You don't need to repeat the RewriteEngine on and RewriteBase / directives.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2021-01-20)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。