.htaccess mostly works but generates a few incorrect urls - .htaccess

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/

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]

Related

301 Redirect subpages to new main page

Hi I am new to redirects.
I can get the basic Redirect 301 /page1/ https://example.com/newpage/ redirects to work fine.
I am having an issue with removing a parent page and all of it's child pages and redirecting everything to a new parent page.
The main redirect works:
Redirect 301 /eye-can-hear/ htps://example.com/procedures-services/
But trying to redirect the sub pages
Redirect 301 /eye-can-hear/hearing-services/ https://example.com/procedures-services/
Is redirecting to /procedures-services/hearing-services/ which doesn't exist.
Here is what the .htaccess file looks like, the 2-7 do not redirect properly:
Redirect 301 /eye-can-hear/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/hearing-services/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/hearing-aids/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/self-test-your-hearing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/schedule-your-hearing-evaluation/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/financing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/dual-sensory-wellness/ https://example.com/procedures-services/
Redirect 301 /about-costello-eye-physicians/community-involvement/ https://example.com/about-costello-eye-physicians/in-the-news/
There are some more redirect's but I can only post 7 links. Further down the .htaccess file after some iThemes security stuff is this:
# Stop wordpress username enumeration vulnerability
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^/?author=([0-9]*)
RewriteRule ^(.*)$ https://example.com/? [L,R=301]
and also this:
# 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
Change this lines (2-7) that do not redirect properly to this rewrite:
RewriteEngine On
RewriteRule ^eye-can-hear\/(.*)$ https://example.com/procedures-services [R=301,L]
It's redirect all subpages of /eye-can-hear/* to https://example.com/procedures-services
This line of code worked for me in the .htaccess file:
RedirectMatch 301 /eye-can-hear/(.*) https://example.com/procedures-services
It redirects the page https://example.com/eye-can-hear/ and its subpages to the URL https://example.com/procedures-services
(Answer after two years, but maybe it helps someone)
In the htaccess file, you should put this redirection after all other redirections of subpages:
Redirect 301 /eye-can-hear/ https://example.com/procedures-services/
Your htaccess should be like this:
Redirect 301 /eye-can-hear/hearing-services/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/hearing-aids/ https://example.com/procedures-services
Redirect 301 /eye-can-hear/self-test-your-hearing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/schedule-your-hearing-evaluation/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/financing/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/dual-sensory-wellness/ https://example.com/procedures-services/
Redirect 301 /eye-can-hear/ https://example.com/procedures-services/

How do 301 redirect with .htaccess?

I have url
/en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage%E2%80%8B
and I try
Redirect 301 /en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage\%E2\%80\%8B /en
or
RewriteCond %{THE_REQUEST} ^\w+ /en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage%E2%80%8B
RewriteRule ^ /en [R=301,L]
or
Redirect 301 /en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage\%E2\%80\%8B /en
But any version don't work. How I can redirect from this url?
You can use this rule as your first rule in site root .htaccess:
RewriteEngine On
RewriteRule ^en/parlour-profile/massage-parlour-anna-berry-massage-louise-sensual-massage\xE2\x80\x8B/?$ /en? [L,NC,R=301]
\xMN is used in RewriteRule pattern to match %MN in URL.

Redirect 301 specific pages and the rest to just one page

First I need to redirect these pages to another page in a different domain
Redirect 301 /example1 http://newdomain.com/test1
Redirect 301 /example2 http://newdomain.com/random1
Note the pages are not the same in the new domain (e.g., /example1 to /test1)
After that, I need redirect the rest of the pages to newdomain.com
E.g., Redirect 301 (everything else) to http://newdomain.com
Try below rule, using mod rewrite I am assuming you have mod rewrite enabled.
RewriteEngine On
RewriteRule ^example1$ http://newdomain.com/test1 [R=301,L]
RewriteRule ^example2$ http://newdomain.com/random1 [R=301,L]
RewriteCond %{REQUEST_URI} !^(example1|example2)
RewriteRule ^ http://newdomain.com [R=301,L]
If you want to use mod-alias , you can use these redirects :
RedirectMatch 301 ^/example1/?$ http://example.com/test1
RedirectMatch 301 ^/example2/?$ http://example.com/random1
#redirect everything else to the homepage of example.com
RedirectMatch ^.+$ http://example.com/
Clear your browser cache before testing these redirects.
Try this in your .htaccess
RewriteEngine On
RewriteRule ^(.*)$ http://newdomain.com/ [R=301]

htaccess 301 redirect with question mark

I have list of URL (600 approximately) that needs to be redirected, few URL are:
Redirect 301 /assets?/elfinder1?/elfinder.html http://www.example.com/swiftkanban/
Redirect 301 /backup?/elfinder?/elfinder.html http://www.example.com/swiftkanban/
Redirect 301 /blog?/xmlrpc.php http://www.example.com/blog/
Redirect 301 /blog.feed?type=rss http://www.example.com/blog/
Redirect 301 /m?/ http://www.example.com/swiftkanban/
Redirect 301 /m/ http://www.example.com/swiftkanban/
Redirect 301 /mobile?/ http://www.example.com/swiftkanban/
Redirect 301 /mobile/ http://www.example.com/swiftkanban/
Redirect 301 /SgjRY?/blog?/16-david-blog?/63-who-owns-kanban.html http://www.example.com/blog/who-owns-kanban/
Redirect 301 /support?/about.html http://www.example.com/about-us/
Redirect 301 /support?/about_us http://www.example.com/about-us/
Redirect 301 /support?/aboutus http://www.example.com/about-us/
Redirect 301 /support?/company http://www.example.com/about-us/the-team/
Redirect 301 /support?/contact.html http://www.example.com/contact-us/
Redirect 301 /support?/contact_us http://www.example.com/contact-us/
Redirect 301 /support?/contactus http://www.example.com/contact-us/
The problem is, I cannot do wildcard redirect, and this redirect rule does not work, I tried escaping the question mark using \ but this does not seem to work, can someone point me to the right direction?
Redirect directive cannot match query string. You need to use mod_rewrite based rules and use a RewriteCond directive as example below:
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog\?/xmlrpc\.php [NC]
RewriteRule ^ http://www.example.com/blog/? [L,NE,R=301]
RewriteCond %{THE_REQUEST} /blog\.feed\?type=rss [NC]
RewriteRule ^ http://www.example.com/blog/? [L,NE,R=301]
Trailing ? after /blog/ is for removing pre-existing query string. If you want query string after redirect then remove ? from target URL.
References:
Apache mod_rewrite Introduction
.htaccess tips and tricks

Basic 301 rediret not working

I am trying to redirect a handful of old blog posts to new ones. Here is my htaccess that I have written and placed in the root directory. Keep in mind the site is now on linux server and not windows.
redirect 301 /public/recipesdetl.aspx?id=1888 http://www.mysite.com/recipes/
redirect 301 /public/recipesdetl.aspx?id=2025 http://www.mysite.com/recipes/?p=405
redirect 301 /public/recipesdetl.aspx?id=2682 http://www.mysite.com/recipes/?p=549
redirect 301 /public/recipesdetl.aspx?id=2152 http://www.mysite.com/recipes/?p=309
redirect 301 /public/recipesdetl.aspx?id=1398 http://www.mysite.com/recipes/?p=817
redirect 301 /public/recipesdetl.aspx?id=1908 http://www.mysite.com/recipes/?p=155
redirect 301 /public/recipesdetl.aspx?id=2162 http://www.mysite.com/recipes/?p=317
redirect 301 /public/recipesdetl.aspx?id=1889 http://www.mysite.com/recipes/?p=145
redirect 301 /public/recipesdetl.aspx?id=1951 http://www.mysite.com/recipes/?p=808
redirect 301 /public/recipesdetl.aspx?id=2032 http://www.mysite.com/recipes/?p=238
redirect 301 /public/recipesdetl.aspx?id=2101 http://www.mysite.com/recipes/?p=269
redirect 301 /public/recipesdetl.aspx?id=2147 http://www.mysite.com/recipes/?p=304
redirect 301 /public/recipesdetl.aspx?id=2563 http://www.mysite.com/recipes/?p=449
redirect 301 /public/recipesdetl.aspx?id=2043 http://www.mysite.com/recipes/?p=243
redirect 301 /public/recipesdetl.aspx?id=2224 http://www.mysite.com/recipes/?p=361
redirect 301 /public/recipesdetl.aspx?id=2151 http://www.mysite.com/recipes/?p=308
You can include query strings in the first URI-path in a Redirect statement, you'll need to use mod_rewrite's %{QUERY_STRING} variable:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=1888$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/ [L,R=301]
RewriteCond %{QUERY_STRING} ^id=2025$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/?p=405 [L,R=301]
RewriteCond %{QUERY_STRING} ^id=2682$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/?p=549 [L,R=301]
RewriteCond %{QUERY_STRING} ^id=2152$
RewriteRule ^/?public/recipesdetl.aspx$ http://www.mysite.com/recipes/?p=309 [L,R=301]
etc.

Resources