.htaccess rule to forward only if old url entered - wordpress

Here's a tricky one for y'all. I have a WordPress site installed on my server in the following directory:
public_html/priestessentrepreneur/dsa/blogdsa
I have divinesparkastrology.com pointed to public_html/priestessentrepreneur/dsa
I have priestessastrology.com pointed to public_html/priestessentrepreneur/dsa/blogdsa
So, my (singular) WordPress installation used to have the site URL set to http://www.divinesparkastrology.com/blogdsa but I recently changed it to http://www.priestessastrology.com/. When you type in http://www.divinesparkastrology.com/ you are correctly forward to the new url, http://www.priestessastrology.com/. However, if you try to go directly to http://www.divinesparkastrology.com/blogdsa it says Nothing Found.
I want http://www.divinesparkastrology.com/blogdsa and all of its subfolders and files to point to their new location at http://www.priestessastrology.com/ (the directory structure hasn't ever changed, only the base URL changed). How do I update the .htaccess file so that the new url still works properly, but if and only if the user has entered the old URL they are forwarded to the new one?
Edit: here's what's currently in my /dsa .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^divinesparkastrology\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.divinesparkastrology\.com$
RewriteRule ^/?$ "http\:\/\/www\.priestessastrology\.com\/" [R=301,L]
And here's what's in my /dsa/blogdsa .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

Add the following RewriteRule to the WordPress .htaccess file:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# added line
RewriteRule ^blogdsa/?$ / [R=301,L]
RewriteRule ^blogdsa/(.+)$ /$1 [R=301,L]
RewriteRule . /index.php [L]
Note: that you will never be able to access a blogdsa WordPress page with this rule in place.

Don't do it at the .htaccess level. Do it at the httpd.conf level, with a very basic
NameVirtualHost x.x.x.x
<VirtualHost x.x.x.x>
ServerName www.divinesparkastrology.com
ServerAlias divinesparkastrology.com *.divinesparkastrology.com
RedirectPermanent / http://priestessastrology.com
</VirtualHost>
This way you can do your redirects without having to mess with sharing .htaccess with wordpress or having the two sites sharing a common structure. Apache will take care of redirecting all requests to the new server name without having to load/parse/rewrite the .htaccess file on every single hit.

Add the following to your the top of your .htaccess file in the public_html/priestessentrepreneur/dsa/blogdsa folder. before any existing rules.
RewriteEngine On
RewriteBase /
#if request is on the divinesparkastrology domain
RewriteCond %{HTTP_HOST} ^(www\.)?divinesparkastrology\.com [NC]
#and it is for a folder starting with blogdsa
RewriteCond %{REQUEST_URI} ^/(blogdsa.*)$ [NC]
#redirect them to the same folder on the priestressastrology site
RewriteRule .* http://www.priestessastrology.com/%1 [L,R=301]
Edit: modified to include just divinesparkastrology without www and, more importantly, since blogdsa is a part of both sites, the .htaccess should be placed in blogdsa directory, not the dsa dir.

Related

subfolder is redirecting to root and it shouldn't

My site is a wordpress site. If a user lands on http://www.example.com/subfolder (non-ssl), they are being incorrectly redirected to https://www.example.com (ssl but without the subfolder).
How do I stop this? They need to stay within the subfolder after being redirected to https://.
I"m using Really Simple SSL plugin within Wordpress. My .htaccess file contains this code:
#BEGIN ReallySimpleSSL
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTPS} !=on [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /subfolder/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /subfolder/index.php [L]
</IfModule>
I've never been great editing the .htaccess file. Any help would be appreciated!
EDIT:..........
My .htaccess file is located within the subfolder. There is another .htaccess file located in the root, along with another website. I'm trying to keep users of the subfolder within the subfolder. *I've added a second IfModule to the above code.
The .htaccess in the root contains this code:
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Running in a fresh browser window, that I haven't used yet, I am getting the same results.
The issue most likely is that rule you implemented atop the configuration file in the subfolder:
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
It gets applied before anything else in the subfolder and it inconditionally rewrites all non-ssl requests to ssl. The issue here:
The capturing pattern ^(.*)$ gets applied to the relative path in the requests, that is only the section of the path from the subfolder base on. That is how rules in distributed configuration files get applied, this is clearly documented. That actually is one of the reasons why distributed configuration files add a lot complexeity and cause so many issues...
So instead you will have to either implement the https-redirection on top level, or use the absolute path or manually add the subfolder to the redirection target. I'd go with the second option:
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The Really simple ssl plugin can cause such issues. Check the stop editing htaccess file option in the plugin settings and test with your htaccess code or try using the below snippet which works for me fine for subfolders/subdomains everything
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"

Hiding directory with htaccess in Wordpress homepage

I just want to hide the /wordpress/ in the URL of my site (http://example.com/wordpress/) with htaccess (I don't want to change the real URLs because I would lose all my social media shares data). I use the following htaccess in the root directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+wordpress/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^wordpress/)^(.*)$ /wordpress/$1 [L,NC]
It works smoothly for all the pages of my website (i.e. if I go to example.com/wordpress/post/ it directly shows example.com/post/). But I have a problem with the homepage. It still always shows as example.com/wordpress/. I can't find the way to hide /wordpress/ here. Can you help me?
More info:
I also have in the /wordpress/ subfolder the following default .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
# END WordPress
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+wordpress/([^\s]+) [NC]
In order to catch requests for /wordpress/ (effectively the root of the WP site) then you would need to change the CondPattern in this directive to allow 0 or more trailing characters (ie. *). Currently, it is looking for 1 or more (ie. +) which excludes the bare directory. In other words:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+wordpress/([^\s]*) [NC]
There are presumably other "WordPress" directives in your .htaccess file that are routing the request. The order of directives are important, so there could still be a conflict.
However, as mentioned in comments, you should consider changing the URL properly in WordPress itself and then redirecting the old URL (with the "wordpress" directory) to the new URL (without "wordpress) in .htaccess. Without changing the URL structure in WP then every internal link on your site is going to result in an external redirect.

How to remove index.php from CodeIgniter3 install in subdirectory with Wordpress on root?

So the title of the question was a little difficult to come up with. Here's the deal, the CI3 install is like this: www.mydomain.com.au/apps/TGPS where TGPS is the CI application. Additionally, on the root of the web folder there is a Wordpress install. My question is, what is the .htaccess setup that I need to compress the URLs from:
www.domain.com/apps/TGPS/index.php/controller/function
to this:
www.domain.com/apps/TGPS/controller/function
At present I have this:
The top level .htacess is this:
# BEGIN WordPress
RewriteRule ^/apps($|/) - [L]
# END WordPress
The .htaccess in TGPS is this:
<IfModule mod_rewrite.c>
RewriteEngine On
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
RewriteCond $1 !^(index\.php|public|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and the .htaccess in application is the standard one.
Seems everything I try either breaks Wordpress, CodeIgniter or has no effect. Any help would be appreciated.
At first glance, you're missing a rewrite base:
RewriteBase /apps/TGPS/
Try bellow code
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^index\.php$ / [L,R=301]
Then the www to non-www:
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]

Presence of .htaccess File in Root Directory When Using Wordpress blog in Subdirectory

I have an .htaccess file in the root directory of all my sites which handles canonical rewrites. The entire content of the .htaccess file is as follows:
Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.domain.com/$1/ [L,R=301]
The trouble is, when I install a Wordpress blog in a subdirectory (/blog) the WP blog becomes inoperable. I cannot log into the wp-admin area. My host tells me that this is because of the .htaccess file in the root. Therefore this needs to be deleted.
So how do I achieve the same canonical URL rewrite instructions if I can't have an .htaccess file in the root? Can I have these instructions in a robots.txt file in the root? Or is there another way of doing this?
If you document root is /var/www/my-site/ and you want to have wordpress in /var/www/my-site/blog you need to do the following:
1.) Create an index.php file the following content and put it into /var/www/my-site
<?php
define('WP_USE_THEMES', true);
require('./blog/wp-blog-header.php');
?>
2.) Put the following .htaccess into /var/www/my-site
# Redirect http://example.com to http://www.example.com
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
# BEGIN WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Now the way you access your admin area is http://www.example.com/blog/wp-admin
Then in your dashboard, go to the settings and set WordPress Address URL to http://www.example.com/blog and the Site URL to http://www.example.com

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