Wordpress 301 permanent redirect with exceptions - wordpress

I'd like to set a permanent redirect whenever someone visits myoldsite.com or its subpages to mynewsite.com like that:
visit myoldsite.com go to mynewsite.com
visit myoldsite.com/subpage go to mynewsite.com (and NOT
mynewsite.com/subpage)
I would also like to make exceptions to the above for wp-admin and wp-login subpages. I don't have server access to myoldsite Wordpress installation, so I can edit .htaccess file only through a Wordpress plugin. I would also like to make an exception for .htaccess if possible, in order to be able to reedit .htaccess via my 'Wordpress .htaccess plugin' once the redirect is done, so I can have the power to revert things back to default if necessary.
My .htaccess file:
# 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

In your htaccess plugin editor, add the following line in your rewrite rules:
RewriteCond %{REQUEST_URI} !^/wp-admin/
RewriteCond %{REQUEST_URI} !^/wp-login\.php
RewriteRule ^(.*)$ http://www.mynewsite.com/ [R=301,L]

Related

.htaccess Do Not Redirect For My IP

I am doing some changes directly to the server on my Wordpress website. I've modified the access file and I want all the users to be redirected excluding my IP. So basicly when I access the website I want it to work normally but when someone else access it I want it to be redirected to a maintenance page.
Here is my current .htaccess file which doesn't work:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^89.137.131.186
RewriteCond %{REQUEST_URI} !^/maintenance/index\.php$
RewriteRule ^(.*)$ http://domain.com/maintenance/index.php [R=307,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Why use a redirect? Why not use this plugin: http://wordpress.org/extend/plugins/ultimate-maintenance-mode/
It's what I use for Maintenance pages. All user's who have logged in will be able to see the site as normal. All non-logged in users will see the maintenance screen which is customizable.

Redirect loop when combining htaccess with permalink wordpress explanation

I have a problem redirecting links using htaccess rewrite. Originally I had only the Rewrite Rule part without the WordPress code underneath and it was working fine. Now, after I tried to add a permalink using Wordpress, the link is giving me a redirect loop problem.
Essentially what I want to do is allow the viewers to go straight to 'www.example.com/blog' when they type 'www.example.com'
I think I am missing something because I thought that the rewriteRule would stop the moment
RewriteRule ^$ /blog [R=301,L]
is processed because of the [L] flag and then other RewriteRules would not be looked at. Can someone help me understand how the server actually go through the steps of the code below?
RewriteEngine on
RewriteRule ^$ /blog [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
You need to put the line:
RewriteRule ^$ /blog [R=301,L]
In the htaccess file in your document root, not in the /blog directory.

htaccess redirect problems, Wordpress and static html site not redirecting properly

I'm having problems with a customer's website. I've created a new Wordpress site for him and now I'm having problems with htaccess redirecting.
Before I installed Wordpress on the server, there was two static basic HTML-sites. One in the public_html root using the main domain and a completely different subsite in a subfolder with a htaccess rewrite rule directing the subdomain into that folder.
Now there is Wordpress messing up the redirects. Wordpress of course made modifications to the original htaccess-file. In addition to that, the customer also had to change servers within the hosting provider to get newer php version so I had to write the htaccess file from scratch.
Here is the current htaccess-file :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /www/index.php [L]
</IfModule>
# END WordPress
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomainfolder/
RewriteRule ^(.*)$ subdomainfolder/$1 [L]
Wordpress in installed in a folder named www and the url is being rewritten, it works beautifully. But I'm having problems with the subdomain. In the code above I've replaced the subdomain url with subdomainurl.com, the site lies in public_html/subdomainfolder/.
Now, when I type www.subdomainurl.com, the redirect works, the site is shown. But www.subdomainurl.index.html typed doesn't. It has to be written www.subdomainurl.com/subdomainfolder/index.html, so I'm clearly not writing the redirect right, adding the subdomain folder as well. I've tried to mingle with that htaccess but can't get it right. Everytime I try to change something I get a 404 in the main domain's Wordpress site.
At the moment these work:
www.maindomain.com/subdomainfolder/index.html
www.subdomainurl.com/subdomainfolder/index.html
And this doesn't, which I'm trying to do:
www.subdomainurl.com/index.html
I'm not a redirect expert, any help would be much appreciated.
Edit/Addon :
Here is the old code from the old server that redirected the subdomain to the subfolder and rewrote the url, it doesn't work anymore so I ditched it.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subdomainfolder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdomainfolder/$1
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteRule ^(/)?$ subdomainfolder/index.html [L]
I don't see an entry in the htaccess file for just the subdomain and for the main domain.
Currently I'm using this code
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /www/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /www/index.php [L]
</IfModule>
END WordPress
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?subdomainurl.com$ [NC]
RewriteCond %{REQUEST_URI} !subdomainfolder
RewriteRule ^(.*)$ subdomainfolder$1
This loads the whole secondary domain (subdomainurl.com) as www.subdomainurl.com/subdomainfolder and it works like that (www.subdomainurl.com/subdomainfolder/index.html, www.subdomainurl.com/subdomainfolder/work.html etc.), but it still isn't what the customer really wanted. I still need to eliminate the subdomainfolder-part from the url by htaccess, so far I haven't had success. I suppose I haven't got it right on the final row of the code.

Wordpress .htaccess www. not forcing

I'm trying to force www. on all the pages of my site.
I have wordpress install in a folder on my main site (/blog). In the /blog folder there is an .htaccess file with the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
In the main dir of the site. I have a .htaccess file that has the follow:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.sitename.com [NC]
RewriteRule (.*) http://www.sitename.com/$1 [R=301,L]
RewriteRule ^blog/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^blog/(.*)$ /blog/index.php [L]
In wordpress is have the option set for www. The www. appears on every page except the when I go straight the the site.com/blog address
Go Settings -> General:
WordPress Address (URL)
Site Address (URL)
And set both of them to www.yoursite.com (instead of yoursite.com), wordpress automagically takes care for setting all the urls to www.yoursite.com and that's it.
If that isn't enough, you shouldn't edit the .htaccess file anyway, because anytime you save or reload your permalink settings, Wordpress will overwrite your .htaccesss (some plugins may do that as well).
If you do want to rewrite your URL, you should do it with WP Rewrite (Codex), or if you want a little more simple approach, there is a good plugin for that called Redirection (Wordpress.org Plugin Directory).
In Redirection plugin you can just set the options to *.mydomain.com/* to redirect to www.mydomain.com/* with a 301, and that will take care of your htaccess as long as you keep the plugin active.
Hope this helps :)
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
In the .htaccess of the WordPress installation (/blog/ in your case), you can add the following code ABOVE the line that reads # BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/subdirectory\/$1" [R=301,L]
</IfModule>
## Redirect primary WordPress URL to WWW version
Obviously, replace "domain.com" with your actual domain name and "subdirectory" with the name of the subdirectory where WordPress is installed. (/blog/ in the OP's request).
Have Option for you:
using wp-config.php add:
define('WP_HOME','http://www.domainname.com');
define('WP_SITEURL','http://www.domainname.com');
using wp-admin
Log into your wp-admin
Browse to ‘Settings’ -> ‘General’
Under ‘WordPress Address (URL)’ and ‘Site Address (URL)’ add www to the address as shown below

Wildcard domain redirect with WordPress

I have two domain names setup with sub-domains as follows...
blog.domain.com
www.blog.domain.com
blog.domain.info
www.blog.domain.info
Both domains are pointing to the same location on the same server, a directory containing WordPress. (domain.com/blog)
To keep Google happy, I want everything to redirect to this one domain...
blog.domain.com
Here is what's inside the .htaccess file contained in the WordPress directory...
# 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
So I went into my cPanel and added a 301 Domain Redirect as follows...
blog.domain.info -> blog.domain.com (with "www" optional and wildcard selected).
cPanel then automatically added the following to the same .htaccess file under the WordPress rewrite rules...
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com$1" [R=301,L]
The problem is that the wildcard portion does not seem to work.
When I go to blog.domain.info, I get redirected to blog.domain.com as expected.
But when I go to blog.domain.info/my-post, I do not get redirected at all.
How can I fix this? I've tried rewrite rules that I know work but all I can think of is that the WordPress rules are interfering.
Once it's fixed, can I move these mod-rewrites to the main .htaccess in the hosting account's www root keeping them separate from the WordPress rules? Edit: Answer- NO, they will not work because they are domains parked in directories off the root www.
Thank-you!
Looks like I simply had to move the new rules above the WordPress section. I also added a new one that is supposed to remove the 'www' from the dot com domain name.
This all seems to be working.
Any comments appreciated.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
EDIT
It should be noted that if you change anything in the htaccess file contained between these Wordpress comments, it might get overwritten by Wordpress at a later time. Moving your custom edits outside AND above this block also works and is immune from any changes to htaccess made by Wordpress itself.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.info$ [OR]
RewriteCond %{HTTP_HOST} ^www.blog.domain.com$
RewriteRule ^(.*)$ "http\:\/\/blog\.domain\.com\/$1" [R=301,L]
# 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 own multiple domains for my site MethodShop, including methodshop.com, methodshop.net, etc.. When doing maintenance, i'll divert traffic between the different sites so my users don't have their experience interrupted. below is the htaccess wildcard script i use. it takes whatever URL the user attempts to access and mirrors that link on another domain.
for example,
http://methodshop.NET/games/play/bubblewrap/index.shtml
would redirect to
http://methodshop.COM/games/play/bubblewrap/index.shtml
here's the htaccess script for methodshop.net that rewrites all methodshop.net URLs to methodshop.com. just edit it for your domain.
RewriteEngine on
RewriteRule (.*)$ http://www.methodshop.com\/$1 [R=301,L]

Resources