301 redirects and wordpress pretty permalinks - php

I am moving from an old php based site to a new one based on wordpress. In the process we are getting rid of quite a few unnecessary pages.
However some of the pages are showing on google, and I want to avoid 403 or 404 errors by using 301 redirects to pages now containing the same info on the new site.
My current .htaccess shows:
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 53
DirectoryIndex index.cgi index.php
# 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
and I was wondering what the best way to do 301 redirects is? putting them before or after the code just gives a 404 error.

This should work;
redirect 301 /old/old.htm http://www.website.com/new.htm

Putting a rule similar to this before the index.php rewrite rules should work:
RewriteRule ^(.*)$ http://newwebsite.com/$1 [R=301,L]
You can put a bunch of RewriteCond's before it to catch only those pages with issues.
For the specific case you posted in the comments, this rule works:
RewriteRule ^why-choose-us.* about-us [R=301,L]

Related

Redirection codes and or keep alive code in htaccess file doesnot work. How to make them work

My site is on wordpress.
This is the code I have written for hatccess file. But the redirection doesnot work. I want to force user redirect into www even if they donot write www inthe address bar. or click on a link which does not have www on it.
So I have added the following 301 redirection at the top of htacess file. but the redirection does not work. it only works with the home page. but not with the other pages. With the home page it works probably because I have set the site url = https://www.inetplc.com
Redirect 301 http://inetplc.com/ https://www.inetplc.com
Redirect 301 http://www.inetplc.com/ https://www.inetplc.com
Redirect 301 https://inetplc.com/ https://www.inetplc.com
Redirect 301 https://inetplc.com/case-studies https://www.inetplc.com/case-studies
Redirect 301 https://inetplc.com/about-inet https://www.inetplc.com/about-inet
I have tried configuring the lines like the following. but then the whole page is down. I dont know why.
Redirect 301 /about-inet https://www.inetplc.com/about-inet
This is the rest of the code in htaccess file.
#This Apache config file was created by Duplicator Installer on 2019-07-19 09:20:24.
#The original can be found in archived file with the name htaccess.orig
# 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
I wrote the code to enable keep alive it also did not work . so I removed that .
# Keep Alive
<ifModule mod_headers.c>
Header set Connection keep-alive
</ifModule>
What I am doing wrong? Wordpress version 5.2.2
It seems you just need to redirect your website from http://inetplc.com or http://www.inetplc.com to https://www.inetplc.com
Add following code in your .htaccess to redirect https://www.inetplc.com
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.inetplc.com/$1 [R=301,L,NE]
# 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

.htaccess redirect index.php only (exclude if variable after)

Long story short, I updated an e-commerce website but had to install the new static CMS system into a sub-directory (/wear). The main e-commerce store is still sitting in the root directory (/), and due to the large amount of products and SEO impact, I need to leave it there.
I would like to setup a requests for just index.php to redirect to /wear/
At the same time, if there is a request for index.php?XXXXX I would like it to still use the index.php file.
I've tried using the following .htaccess code but it's redirecting everything. Can anyone help me with this? I apologize for asking this as I know there are multiple threads, but none seemed to provide a good answer.
Attempt 1
RedirectMatch /index.php https://domain/wear/
Attemp 2
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wear/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
The issue comes from your RewriteBase. When setting it up to /wear/, RewriteRule ^index\.php$ - [L] actually translates to RewriteRule ^wear/index\.php$ - [L] which is not really what you want.
I would try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^index\.php$ /wear/ [L,R=301]
What it does is: check that the query string is empty, and if it is, redirect index.php (at the beginning of the request URI, so /index.php only) to /wear/
You will also need to make sure than mod_rewrite is active.
To do so, you can remove the <IfModule mod_rewrite.c> and </IfModule> parts. If mod_rewrite is not available, it will trigger an Error 500 as the RewriteEngine command will not be recognised.

'Fetch as Google' in Webmaster tool says 'Redirected'

Google webmaster tool shows me this for my website. https://ibb.co/cnCOfk
This is a WP website, and my guess is it's because the redirection from non-www to www is not happening. Google fetches with www and then my website gets redirect too non-www domain. How do I fix this problem?
I found online that I can solve this with .htaccess. Now, I added last 2 lines (above IfModle ends). Currently, my .htaccess looks like 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]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# END WordPress
Once I add this, website goes in infinite loop of "www.domainname.co" to "domainname.co" to "www.domainname.co" and so on.
How do I solve this problem? Thank you in advance.
It would seem this redirect is occurring within WordPress itself - that is where you need to fix this. You can then repeat the same redirect in .htaccess if you wish, as that would be more efficient.
But if you do the opposite redirect in .htaccess then you'll naturally get a redirect loop.
EDIT: And if you implement this redirect in .htaccess, it should go before the WordPress front controller.

Parallel Plesk - Getting a 404 error on my wordpress homepage

I am getting Error 404 - Page not found on my Wordpress' homepage. I think the rewrite rules are fine 'cause I can see the other page -http://accuratewatch.com/
But if I directly call the index.php file, I will get the desired result - http://accuratewatch.com/index.php
I think the rewrite rules are fine 'cause I can still see the other pages. I even put the redirect rule but it's not working. Here's the copy of my .htaccess
DirectoryIndex index.php index.html
Redirect / index.php
# 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
I am using Parrallel Plesk and the site was working with .asp before so I override it with index.php.
Can you help me figure out the source of the problem?

redirecting all dynamic urls to index page

as i want to redirect all dynamic urls like http://www.mysite.com/play.php?id=4820 to http://www.mysite.com/
i have so many dynamic urls i dont want my user to see the page like page not found etc. so whenever a user try to access the dynamic url like the above he should be redirected to home page. please can anyone tell me how to achieve this using .htaccess
Regards,
phphuger.
Enable mod_rewrite and put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+play\.php[\s\?] [NC]
RewriteRule ^ / [L,R]
I think this should do.
RewriteRule ^\?.*$ http://www.mysite.com
EDIT2
Ok, now I think understand your question.
You have url at play.php that used to handle all your urls and you want them to be redirected to the homepage.
Here are two possible solutions. This is the first:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
The above will redirect any requests to play.php to your home page using a 301 redirect, which means the redirect is permanent. This is different than a 302 redirect which is temporary. So it is important to redirect using 301.
Also in this rewrite rule, we don't care if the url exists or not. So you don't need to have the actual play.php file or anything. It just matches based on the url.
On the line for RewriteRule, there is a question mark at the end, this is to erase the query string from the url on redirect. I'm assuming you don't want the query string to carry over.
The [NC] is for case insensitive matching, so /PlAy.php would be redirected also
An alternative is use the Redirect directive:
Redirect 301 /play.php http://www.mysite.com/
This will do a 301 permanent redirect to your homepage if the user tries go to play.php. The only downside of this is that the query string shows up. You can add a question mark at the end and it will erase the query string. Unfortunately the question mark stays.
If you happen to have multiple endpoints and not just play.php, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play\.php* [NC, OR]
RewriteCond %{REQUEST_URI} ^/another_url\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
This RewriteRule will match play.php or another_url.php and do 301 redirect to your home page.
Alternatively:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(play|another_url)\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
Here is the alternative using the Redirect directive
Redirect 301 /play.php http://www.mysite.com/
Redirect 301 /another_url.php http://www.mysite.com/
Alternative you could also use the RedirectMatch directive to use regex expressions (Note I haven't actually tested this one)
RedirectMatch 301 /(play|another_url)\.php http://www.mysite.com/
EDIT
Ignore this answer. I misread your question. The following redirects all requests to index.php unless the file or directory exists, which is not what you want now that I read your question.
Here is how Wordpres does it. I believe Zend and Joomla also have variations of 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

Categories