Search code examples
.htaccessmod-rewritehttp-redirecthttp-status-code-301

.htaccess mostly works but generates a few incorrect urls


I'm migrating a website to a different root domain and I'm trying to redirect all old urls to the new ones. Most urls are redirected correctly but a couple get scrambled up. For instance pythonforspss.org/solutions redirects to http://www.spss-tutorials.com/python/solutions rather than http://www.spss-tutorials.com/python.

Since the target urls seem pretty clear to me, I just don't get what's going wrong. It may have to do with the Rewrite rules but I don't see how to fix it.

UPDATE

I tried a lot of modifications but the symptoms are still the exact same. The entire .htaccess (I apologize for the length of it) at this point is:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
RewriteRule ^get-started$ http://www.spss-tutorials.com/python/ [L,R=301]
RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
RewriteRule ^solutions$ http://www.spss-tutorials.com/python/ [L,R=301]
RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
RewriteRule ^spss-tips$ http://www.spss-tutorials.com/basics/ [L,R=301]
RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
RewriteRule ^glossary$ http://www.spss-tutorials.com/glossary/ [L,R=301]
RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
RewriteCond %{THE_REQUEST} \ /.*index\.php
RewriteRule ^index\.php$ http://www.spss-tutorials.com/python/ [L,R=301]
RewriteRule . /index.php [L]
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
</IfModule>

#First redirect single pages
Redirect 301 /how-to-write-very-short-syntax http://www.spss-tutorials.com/write-shorter-syntax/
Redirect 301 /how-to-suffix-all-variable-names http://www.spss-tutorials.com/suffix-all-variable-names/
Redirect 301 /five-essential-data-checks http://www.spss-tutorials.com/five-essential-data-checks/
Redirect 301 /the-twenty-best-keyboard-shortcuts-for-spss http://www.spss-tutorials.com/the-twenty-best-keyboard-shortcuts-for-spss/
Redirect 301 /assemble-your-own-toolbar http://www.spss-tutorials.com/assemble-your-own-toolbar/
Redirect 301 /disclaimer http://www.spss-tutorials.com/disclaimer/
Redirect 301 /select-variables-by-variable-labels http://www.spss-tutorials.com/select-variables-by-variable-labels/
Redirect 301 /five-reasons-for-not-relying-on-the-journal-file http://www.spss-tutorials.com/five-reasons-for-not-relying-on-the-journal-file/
Redirect 301 /introducing-python-4-installing-and-testing http://www.spss-tutorials.com/introducing-python-4-installing-and-testing/
Redirect 301 /create-your-own-toolbar-tools http://www.spss-tutorials.com/create-your-own-toolbar-tools/
Redirect 301 /concatenate http://www.spss-tutorials.com/concatenate/
Redirect 301 /indentation http://www.spss-tutorials.com/indentation/
Redirect 301 /leave http://www.spss-tutorials.com/leave/
Redirect 301 /convert-a-number-as-a-date-into-a-date-variable http://www.spss-tutorials.com/convert-a-number-as-a-date-into-an-spss-date/
Redirect 301 /introducing-python-2-how-it-basically-works http://www.spss-tutorials.com/introducing-python-2-how-it-basically-works/
Redirect 301 /set-decimals-for-output http://www.spss-tutorials.com/set-decimals-for-output/
Redirect 301 /compute-age http://www.spss-tutorials.com/compute-age/
Redirect 301 /date-variable http://www.spss-tutorials.com/date-variable/
Redirect 301 /modulus http://www.spss-tutorials.com/modulus/
Redirect 301 /how-to-convert-a-unix-date-to-an-spss-date http://www.spss-tutorials.com/convert-unix-dates-into-spss-dates/
Redirect 301 /time-variable http://www.spss-tutorials.com/time-variable/
Redirect 301 /how-to-convert-dates-from-google-analytics http://www.spss-tutorials.com/convert-google-analytics-dates-into-spss-dates/
Redirect 301 /how-to-extract-a-year-from-a-date http://www.spss-tutorials.com/extract-a-year-from-a-date/
Redirect 301 /use-the-command-syntax-reference http://www.spss-tutorials.com/command-syntax-reference/
Redirect 301 /change-your-working-directory http://www.spss-tutorials.com/change-your-working-directory/
Redirect 301 /select-variables-having-pattern-in-names http://www.spss-tutorials.com/select-variables-having-pattern-in-names/
Redirect 301 /split-string-variable-into-components http://www.spss-tutorials.com/split-string-variable-into-components/
Redirect 301 /find-within-subjects-favorite-over-several-variables http://www.spss-tutorials.com/find-within-subjects-favorite-over-several-variables/
Redirect 301 /prefix-many-value-labels http://www.spss-tutorials.com/prefix-many-value-labels/
Redirect 301 /strip-prefix-from-value-labels http://www.spss-tutorials.com/strip-prefix-from-value-labels/
Redirect 301 /rename-or-prefix-many-files http://www.spss-tutorials.com/rename-or-prefix-many-files/
Redirect 301 /move-all-files-from-subfolders-to-main-folder http://www.spss-tutorials.com/move-all-files-from-subfolders-to-main-folder/
Redirect 301 /copy-all-files-from-subfolders-to-main-folder http://www.spss-tutorials.com/copy-all-files-from-subfolders-to-main-folder/
Redirect 301 /find-which-syntax-files-contain-some-expression http://www.spss-tutorials.com/find-which-syntax-files-contain-some-expression/
Redirect 301 /add-filenames-to-files-before-merging http://www.spss-tutorials.com/add-filenames-to-files-before-merging/
Redirect 301 /adjust-string-lengths-before-merging-files http://www.spss-tutorials.com/adjust-string-lengths-before-merging-files/
Redirect 301 /merge-many-data-files http://www.spss-tutorials.com/merge-many-data-files/
Redirect 301 /regression-over-many-dependent-variables http://www.spss-tutorials.com/regression-over-many-dependent-variables/
Redirect 301 /create-dummy-variables http://www.spss-tutorials.com/create-dummy-variables/
Redirect 301 /mean-center-many-variables http://www.spss-tutorials.com/mean-center-many-variables/
Redirect 301 /export-output-to-different-folders http://www.spss-tutorials.com/export-output-to-different-folders/
Redirect 301 /insert-values-from-output-tables-into-text http://www.spss-tutorials.com/insert-values-from-output-tables-into-text/
Redirect 301 /loop http://www.spss-tutorials.com/loop/  
Redirect 301 /macro http://www.spss-tutorials.com/macro/
Redirect 301 /module http://www.spss-tutorials.com/module/
Redirect 301 /path http://www.spss-tutorials.com/path/
Redirect 301 /transformation-command http://www.spss-tutorials.com/transformation-command/
Redirect 301 /procedure http://www.spss-tutorials.com/procedure/
Redirect 301 /substring http://www.spss-tutorials.com/substring/
Redirect 301 /string http://www.spss-tutorials.com/string/
Redirect 301 /strip-prefix-from-variable-labels http://www.spss-tutorials.com/strip-prefix-from-variable-labels/
Redirect 301 /managing-variable-properties-1-introduction http://www.spss-tutorials.com/changing-variable-properties-1-introduction/
Redirect 301 /managing-variable-properties-2-names http://www.spss-tutorials.com/changing-variable-properties-2-names/
Redirect 301 /managing-variable-properties-3-type http://www.spss-tutorials.com/changing-variable-properties-3-type/
Redirect 301 /managing-variable-properties-4-width-and-decimals http://www.spss-tutorials.com/changing-variable-properties-4-width-and-decimals/
Redirect 301 /managing-variable-properties-5-variable-and-value-labels http://www.spss-tutorials.com/changing-variable-properties-5-variable-and-value-labels/
Redirect 301 /managing-variable-properties-6-missing-values-and-more http://www.spss-tutorials.com/changing-variable-properties-6-missing-values-and-more/
Redirect 301 /escape-sequence http://www.spss-tutorials.com/escape-sequence/
Redirect 301 /cautionary-note-on-sums http://www.spss-tutorials.com/spss-sum-cautionary-note/
Redirect 301 /cautionary-note-on-the-add-files-command http://www.spss-tutorials.com/spss-add-files-cautionary-note/
Redirect 301 /how-to-sort-values-within-cases http://www.spss-tutorials.com/sort-values-within-cases/
Redirect 301 /extension-command http://www.spss-tutorials.com/extension-command/
Redirect 301 /read-and-merge-multiple-sheet-excel-workbooks http://www.spss-tutorials.com/read-and-merge-multiple-sheet-excel-workbooks/
Redirect 301 /xlrd http://www.spss-tutorials.com/xlrd/
Redirect 301 /compare-dictionaries-over-files-before-merging http://www.spss-tutorials.com/compare-dictionaries-over-files-before-merging/
Redirect 301 /how-to-disaggregate-weighted-data http://www.spss-tutorials.com/disaggregate-data/
Redirect 301 /any http://www.spss-tutorials.com/any/
Redirect 301 /missing-values http://www.spss-tutorials.com/missing-values/
Redirect 301 /assumption-of-equal-intervals http://www.spss-tutorials.com/assumption-of-equal-intervals/
Redirect 301 /variable-type http://www.spss-tutorials.com/variable-type/
Redirect 301 /compute-a-is-b-is-c http://www.spss-tutorials.com/compute-a-is-b-is-c/
Redirect 301 /reverse-code-variables-with-value-labels http://www.spss-tutorials.com/reverse-code-variables-with-value-labels/
Redirect 301 /spss-recode-command-cautionary-note http://www.spss-tutorials.com/spss-recode-command-cautionary-note/
Redirect 301 /aggregate http://www.spss-tutorials.com/aggregate/
Redirect 301 /very-first-steps http://www.spss-tutorials.com/spss-very-first-steps/
Redirect 301 /names-and-labels-in-output http://www.spss-tutorials.com/names-and-labels-in-output/
Redirect 301 /hire-us http://www.spss-tutorials.com/hire-us/
Redirect 301 /home http://www.spss-tutorials.com/home/
Redirect 301 /introducing-python-1-what-and-why http://www.spss-tutorials.com/introducing-python-1-what-and-why/
Redirect 301 /introducing-python-5-five-essential-basics http://www.spss-tutorials.com/introducing-python-5-five-essential-basics/
Redirect 301 /introducing-python-6-four-tips http://www.spss-tutorials.com/introducing-python-6-four-tips/
Redirect 301 /introducing-python-3-how-to-use-it http://www.spss-tutorials.com/introducing-python-3-how-to-use-it/
Redirect 301 /the-six-greatest-benefits-of-using-spss-syntax http://www.spss-tutorials.com/the-six-greatest-benefits-of-using-spss-syntax/

#Ancient static html
Redirect 301 /getting-started.html http://www.spss-tutorials.com/python/
Redirect 301 /solutions.html http://www.spss-tutorials.com/python/

Solution

  • None of those redirects work for me without having a leading slash:

    Redirect 301 /solutions http://www.spss-tutorials.com/python/
    

    But it's not always a good idea to mix mod_rewrite and mod_alias directives (Redirect is part of mod_alias) because they both get applied to the same URI and you don't always want that happening. Try changing all the Redirects to:

    RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
    RewriteRule ^get-started$ http://www.spss-tutorials.com/python/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
    RewriteRule ^solutions$ http://www.spss-tutorials.com/python/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
    RewriteRule ^spss-tips$ http://www.spss-tutorials.com/basics/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
    RewriteRule ^glossary$ http://www.spss-tutorials.com/glossary/ [L,R=301]
    
    RewriteCond %{HTTP_HOST} pythonforspss\.org$ [NC]
    RewriteCond %{THE_REQUEST} \ /.*index\.php
    RewriteRule ^index\.php$ http://www.spss-tutorials.com/python/ [L,R=301]