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]

# 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
# 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?

  • By "doesn't work", do you mean nothing happens or are you getting an error or incorrect redirect? Are there any other directives in your .htaccess file? Do you have any other .htaccess files in these subfolders? In the network traffic, are you seeing a "301" or some other status? Try changing 301 to 302 and clear your browser cache, has the response code changed in the network traffic? In short... the code you posted should already do what you require, so there must be "something else" going on.
    – MrWhite
    Jan 20 at 12:29
  • @MrWhite, the point is: With my actual code if i access example.com everything works fine, and a 301 redirection occurs. however if i access to a subdirectory (example.com/subdirectory) nothing occurs and i'm still accessing to the old domain (example.com instead of new-example.com) Jan 20 at 12:43
  • Yes, I got that from your question - the "document root" redirects OK. And example.com and www.example.com work the same in this respect it seems. What about the questions I raised in my previous comment... any other .htaccess files etc.? As I said, the problem is not with the rules you posted, so "something else" is causing this not to work. Do you have access to the server config?
    – MrWhite
    Jan 20 at 13:00
  • You've tagged your question "WordPress" - so presumably you have other directives in your .htaccess file?
    – MrWhite
    Jan 20 at 13:01
  • 1
    @MrWhite, I have updated the question with the whole file. I think that there is not another .htaccess. Jan 20 at 13:10

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.


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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.