Reputation: 1361
My .htaccess code below is giving unexpected results. Please help me to fix this by referring expected result vs output result.
<IfModule mod_rewrite.c>
Redirect /main-page https://example.com/new-main-page
Redirect /main-page/sub-page1 https://example.com/new-main-page/new-sub-page1
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
## Added as per WP-1252 ##
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=edge"
</IfModule>
Expected Behavior:
Request 1: https://sample.com/main-page redirect to https://example.com/new-main-page
Request 2: https://sample.com/main-page/sub-page1 redirect to https://example.com/new-main-page/new-sub-page1
Request 3: https://sample.com/main-page/sub-page2 redirect to https://sample.com/page-not-found
Output Result:
Request 1: https://sample.com/main-page redirects to https://example.com/new-main-page (Expected Behavior)
Request 2: https://sample.com/main-page/sub-page1 redirects to https://example.com/new-main-page/new-sub-page1 (Expected Behavior)
Request 3: https://sample.com/main-page/sub-page2 redirects to https://example.com/new-main-page/sub-page2 (Unexpected Behavior. Why?)
Upvotes: 0
Views: 30
Reputation: 784918
This is the way Redirect
works as it catches everything that starts with the given URI string. So Redirect /main-page
will redirect all URIs that start with /main-page
. You should be using RewriteRule
that allows use of regex for precise matching and moreover you're already using that in rest of your .htaccess.
You may use:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^main-page/?$ https://example.com/new-main-page [L,R=301,NC]
RewriteRule ^main-page/sub-page1/?$ https://example.com/new-main-page/new-sub-page1 [L,R=301,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
## Added as per WP-1252 ##
<IfModule mod_headers.c>
Header set X-UA-Compatible "IE=edge"
</IfModule>
Make sure to clear your browser cache or use a new browser to avoid old browser cache.
Upvotes: 1