【问题标题】:.htaccess mostly works but generates a few incorrect urls.htaccess 大部分都有效,但会生成一些不正确的 url
【发布时间】:2013-10-25 06:20:13
【问题描述】:

我正在将网站迁移到不同的根域,并尝试将所有旧 URL 重定向到新 URL。大多数网址都被正确重定向,但有几个网址被打乱了。例如pythonforspss.org/solutions 重定向到http://www.spss-tutorials.com/python/solutions 而不是http://www.spss-tutorials.com/python

由于目标网址对我来说似乎很清楚,我只是不明白出了什么问题。它可能与Rewrite 规则有关,但我不知道如何解决它。

更新

我尝试了很多修改,但症状仍然完全相同。此时整个.htaccess(我为它的长度道歉)是:

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

【问题讨论】:

    标签: .htaccess mod-rewrite redirect http-status-code-301


    【解决方案1】:

    如果没有前导斜杠,这些重定向都不适合我:

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

    但混合 mod_rewrite 和 mod_alias 指令(Redirect 是 mod_alias 的一部分)并不总是一个好主意,因为它们都应用于同一个 URI,而您并不总是希望这种情况发生。尝试将所有 Redirects 更改为:

    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]
    

    【讨论】:

    • 谢谢你,但它还没有工作。我应该both 将斜杠添加到Redirect 规则 将这些RewriteCond 行添加到Rewrite 部分?我这样做了,但似乎并没有改变问题。我应该发布整个.htaccess 文件吗?它相当长(119 行)...
    • @RubenGeert 您需要删除所有Redirect 语句并将它们替换为上述 mod_rewrite 规则。规则必须在您拥有的任何其他规则之前(尤其是 wordpress 规则)
    • 我不知道为什么,但现在看来一切正常!也许是Filezilla 的刷新,但我认为这不会对服务器上发生的事情产生任何影响。无论如何,一百万!
    猜你喜欢
    • 1970-01-01
    • 2018-10-04
    • 2016-07-10
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 2016-12-25
    • 2014-05-10
    • 2019-07-31
    相关资源
    最近更新 更多