Hiding directory with htaccess in Wordpress homepage - wordpress

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.

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;"

Force https+www in WordPress and non-WordPress subdirectory

I need to force https and www with .htaccess, and I'm looking for a solution that is not domain-specific, but will apply to any domain and any folder (using server variables).
The setup:
domain.com has WordPress installed
domain.com/folder2 is a physical folder and contains a separate site
The .htaccess file in domain.com currently contains:
# force www+https
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !on$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
# 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
This seems to work for domain.com, but not for domain.com/folder2: while https is enforced there, the www redirect doesn't work.
I tried placing a second .htaccess file in domain.com/folder2; that doesn't do anything either.
Questions:
How can I make these rules apply to domain.com/folder2 as well?
I've kept the WordPress block separate because WordPress would probably duplicate it when e.g. permalinks are changed via admin, and it doesn't find this block as-is. Is this correct, or is there a better way to consolidate the two blocks?
Thanks!
In .htaccess in /folder2, add the following :
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www [OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]
Delete your /folder2/.htaccess if you want to place this in your root dir.

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.

.htaccess rule to forward only if old url entered

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.

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