Apache doesn't redirect subfolders

Apache doesn't redirect subfolders - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, wordpress, apache, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


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?


Solution :

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.


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect