301 redirection help on wordpress - regex

I'm searching for a 301 redirect rules in .htaccess for the last one week. I have visited many similar pages here too. But all are not working for me. So I'm asking help from masters like you.
I'm using wordpress blogging platform. Before I was using Joomla and that time so many 404 errors was there on my website. There are more than 10,000s of 404 errors. Mainly two category of 404 errors. Examples are example.com//component/option,com_xmap/Itemid,44/component/xxx/yyy/menu1/menu2/something/ and example.com/index.php/component/option,com_xmap/Itemid,44/component/xxx/yyy/directory/menu1/menu2/something.html. Like these too many urls. I think this happened due to some cache problem in Joomla and these urls were created one by one on the homepage of a module. After I disabled cache for that module the problem was solved, but google indexed all these bad urls already.
I think it's better to do a 301 redirect for those two categories of 404 error urls to the homepage or to www.freezonal.com/sitemap.xml since these urls are only indexed by google and are not seen in search results.
Currently I'm using this on my .htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
Is there any 301 redirection code for redirecting those two categories of 404 urls to the main website and is there any other suggestion for better SEO. I'll be very thankful to you, if you solve this.

First, WordPress' rewrite rules must be on the bottom, because they slurp up any 404s. Anything after them isn't going to work correctly. So add new rules above WordPress' rules.
RewriteEngine On
RewriteBase /
RewriteRule (index.php/|/)?component/option http://example.com/ [L,R=301]
Something like that should work. I don't know if you want to keep any of your /component/ URLs working, so you may need to remove the question mark from the RewriteRule.

In order for the rewrite to work, make sure that you wrap your rewrite conditions in the mod_rewrite.c tags. Then turn turn on RewriteEngine and set your RewriteBase. I don't think the redirects work outside of the mod_rewrite tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} .*
RewriteRule ^index.php/(.*)$ /? [R=301,L]
</IfModule>
If you only want to only redirect these two paths to the homepage, try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^component* / [R=301,L]
RewriteRule ^index.php/component* / [R=301,L]
</IfModule>
Try that out and see if it works.
Also, be sure to check out the documentation on apache.org here http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

HTAccess Redirect specific URLs to new domain pages and then rest to Root

Having checked other questions and trying the suggested solutions, nothing has worked so far.
I'm trying to redirect certain URLs from the old-domain to URLs on the new-domain, not necessarily with the same page names. The rest of the URLs should be redirected to the root of the new-domain. This is what I've tried. The redirecting of all pages to the root of the new-domain works, just not the individual pages:
RewriteEngine on
Redirect 301 /travel/ferry.html http://www.new-domain.com/ferry/
RewriteEngine off
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Thank you.
Don't mix Redirect directive and RewriteRule directives as they come from different Apache modules and their order of execution might be unpredictable.
You may have your rules as this:
RewriteEngine on
# keep specific redirect here
RewriteRule ^travel/ferry\.html$ http://www.new-domain.com/ferry/ [L,NC,R=301]
# redirect rest of the URLs to root
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Make sure to test it in a new browser or test after fully clearing browser cache.

Regexp in .htaccess

I've got a simple .htaccess requirement that I'm failing on and would appreciate a pointer.
Content from an old CMS is being migrated to WordPress. The old CMS posts are formatted in the following way:
{postid}-example-post-title
eg
101-this-is-an-old-post
I have a mapping table that links the old post ids to the new WordPress post ids, so in theory \{oldid-*} should map nicely to the WordPress standard \?p={newid} format. I expected the following to work to redirect old url 101-this-is-the-post to the WordPress post 1660:
Redirect 301 /101-.*$ /?p=1660
The regexp /101-.*$ matches correctly, but for some reason my .htaccess file doesn't seem to recognise the regular expression. Here's my full htaccess file:
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 56
DirectoryIndex index.cgi index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Redirect 301 /101-(.*) /?p=1660
I've tried moving the Redirect to the top of the file, the bottom of the file and between the IfModule block to no avail. I'm clearly missing something - any thoughts appreciated
As per julp's comment above, the solution to this problem was to use RewriteRule. The following worked correctly, placed withing the IfModule block :
RewriteRule 301 /101-(.*) /?p=166

.htaccess redirect based on country and language

I have already asked a question about .htacess rules and everything works fine.
I've also found solutions to 301 redirect users based on detected language (HTTP:Accept-Language), but I haven't found answer to how can I redirect users from pt-PT, pt-BR, en-US to specific main pages:
www.example.com/pt-pt/inicio.html
www.example.com/pt-br/inicio.html
www.example.com/en-us/home.html
Here is my working .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# This is for not allowing access from libwww-perl User-Agent
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* – [F,L]
# This is for IP Canonicalization
RewriteCond %{HTTP_HOST} ^999\.999\.999\.999
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# This is for URL Canonicalization
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
# This is from another StackOverflow question
RewriteRule ^([^/]+)/(.+?)\.html?$ index.php?lang=$1&page=$2 [NC,L,QSA]
</IfModule>
AddHandler application/x-httpd-php55 .php .php5 .php4 .php3
How can I achieve that? I'm a little affraid of touching the .htaccess file...
Edit: Does the order of lines makes difference until IP canonicalization? The website is in a shared host and it hasn't passed the test on this for SEO. Also, the root PHP page is already detecting the language and I could make the redirect, but I guess .htaccess would be a more elegant and efficient way to do it.

Wordpress: htaccess redirect (301) doesnt work

I have a task which drives me nuts: I have an old blog and want to create a 301 redirect to my new blog for several posts because I moved some of the content to the new domain.
Let's say one of my old posts is to be find at example.com/this-is-my-post and visitors should be redirected to example.org/my-old-post-2817.
What I tried is this:
# BEGIN Redirect
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post$ http://example.org/my-old-post-2817 [R=301]
</IfModule>
# END Redirect
The problem is that there is no redirect in FF (desktop), Chrome (Android) and standard internet browser (Android). In IE (desktop) I get an error stating that the page can't be displayed. No reason is given.
I made sure not to have any unneccessary blank lines in the .htaccess and, to be on the safe side, deleted my browser's histories, caches and so on.
A CCA of my provider dropped me a message that he re-saved the mod rewrite module and that I have to try it 15 minutes later again. With no success.
So, what did I wrong?
Regards,
Crunchy
Change order of rules to keep R=301 rules before internal WP rules:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^this-is-my-post/?$ http://example.org/my-old-post-2817 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

unable to come up with htaccess rewrite engine

I want to redirect domain1.com to domain2.com in all cases except one particular case:
domain1.com/subfolder/index.php
I want this domain1.com/subfolder/index.php to be intact and not get redirected to domain2.com because I have hundreds of users already bookmarked this page.
But anything and everything besides that domain1.com/subfolder/index.php, I want domain1.com to be redirected to domain2.com
Please help.
Have the following .htaccess in your web root /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)domain1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
Use just [L] if you want the redirection to be transparent i.e. without letting your visitors know.
Use !^/subfolder/.*$ if you want to stop redirection for the complete folder as well as its contents.
You can use RewriteCond to check to see the requested uri is anything but the one you want to redirect, capture the desired path and then redirect to the second domain.
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$
RewriteRule (.*) http://domain2.com/$1