Wordpress RewriteRule Conflicts with .passwd - wordpress

In my root .htaccess file I have the following Wordpress code:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
Slightly different to the standard code because the Wordpress files are in a "wordpress" directory but rewrites still appear relative to the domain root. For example a blog post can be called site.com/this-is-a-blog-post.php and the rewrite works.
This works great except when I want to use htpasswd to protect a directory on the site. If I add the following code to a .htaccess file in any directory I get a 404 when trying to browse to that directory:
AuthName "Private"
AuthUserFile "/home/passwd"
AuthType Basic
require valid-user
This code works correctly when the last line of the Wordpress rewrite code is commented out, but with the Wordpress code intact my password protected directory gives a 404.
So I have a conflict between the two .htaccess files somehow. I have tried writing a bunch of RewriteCond's to try and exclude the subdirectory with the password applied but can't nail it.

Ok, Murphy's Law, just worked it out after posting the question!
The extra condition needed for the Wordpress rewrite is:
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|#.*|\?.*|/[^.]*)$ [NC]
So the complete Wordpress rewrite code becomes:
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (/|\.htm|\.php|\.html|#.*|\?.*|/[^.]*)$ [NC]
RewriteRule . /wordpress/index.php [L]
Now I'll go finish my humble pie.

Related

RewriteCond is not considered in default WordPress htaccess

Good morning,
I have a very strange issue with the default .htaccess with WordPress.
I have a "stats" folder in the webroot which contain something else than WordPress. Usually, I can access it by doing www.example.com/stats/ on the web browser. But with one WordPress, www.example.com/stats/ is rewrited to the index.php of WordPress.
Please note that I use the defaut WordPress .htaccess which is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Usually, RewriteCond %{REQUEST_FILENAME} !-d should help to skip rewriting as stats is a directory. But stats still rewrite to index.php.
I already tried to add some other RewriteCond (with REQUEST_FILENAME and QUERY_STRING) to try skipping manually stats directory but it not works. Here are them (as requested on comments):
RewriteCond %{REQUEST_FILENAME} !^stats$
RewriteCond %{REQUEST_FILENAME} !^stats/$
RewriteCond %{REQUEST_FILENAME} !^/stats/$
RewriteCond %{QUERY_STRING} !stats
RewriteCond %{QUERY_STRING} !^stats/$
RewriteCond %{QUERY_STRING} !^/stats/$
RewriteCond %{QUERY_STRING} !stats(.*)
Both are not working.
As requested in comment also, I tried to rename stats to another name. And it works with the name stats2. But I still want to use stats as folder name.
Note that it is not a caching issue.
Inside the folder stats, there is a .htaccess:
AuthType Basic
AuthName "Members Only"
AuthUserFile /var/www/clients/client0/web1/web/stats/.htpasswd_stats
require valid-user
Note that the folder stats is managed by ISPConfig.
Any idea to check? Thanks in advance.
Found the issue!!!
In general, Apache does the rewrite phase before the authorization phase, which is why your code performs the rewrite without ever asking for user to authenticate.
https://stackoverflow.com/a/13295036/7664726
I had to add it to the .htaccess in the website root:
RewriteCond %{LA-U:REMOTE_USER} !^$
So my final .htaccess is this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{LA-U:REMOTE_USER} !^$
RewriteRule . /index.php [L]
</IfModule>
Thanks to add peoples in comment section helping me solve this issue.
Instead of modifying the root .htaccess file, it may be preferable to simply disable the rewrite engine in the /stats/.htaccess file. Since mod_rewrite is not inherited (by default) this should prevent the WordPress front-controller in the root .htaccess file being processed.
For example, in /stats/.htaccess:
RewriteEngine Off
AuthType Basic
: etc.

Exclude directory from wordpress rewrite issue

In /public_html/ I have a wordpress install as well as a directory - 'mydirectory' - that I'd like to protect and be able to access directly. I've got the protection setup with an .htaccess and .htpasswd file, but I'm only getting 404 errors when I try to access the files in that folder. I've tried just about every re-write option I can find for the root level .htaccess file, but nothing has worked. I'm curious if I'm missing something outside of the rewrite condition that could be affecting it? I cannot seem to figure out why I can't get this to work! Below are my various .htaccess files and the rewrites i've tried.
Root Level:
# 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 in mydirectory:
ErrorDocument 401 default
AuthType Basic
AuthName "LampreyTrillo"
AuthUserFile file:///public_html/mydirectory/.htpasswd
Require valid-user
.htpasswd in mydirectory:
test:pass
Rewrites I've Tried:
RewriteRule ^mydirectory(/.*)?$ - [L,NC]
# disable the rewrite engine in the ignored directory .htaccess
RewriteEngine off
RewriteCond %{REQUEST_URI} ^/mydirectory/(.*)$
RewriteCond %{REQUEST_URI} !(mydirectory) [NC]
#Change last rewrite rule from '. /index.php' to './ /index.php'
RewriteRule ^mydirectory/.*$ - [PT]
RewriteCond %{REQUEST_URI} !^/(mydirectory|mydirectory/.*)$
I'm stumped, cant figure out what I'm missing here so any advice or new information would be greatly appreciated! Thank you in advance!
Add this to your .htaccess file:
RewriteCond %{REQUEST_URI} !(your_excluded_directory|your_excluded_directory_2|your_excluded_directory_3_etc) [NC]
just before
RewriteCond %{REQUEST_FILENAME} !-f

Permalinks issue with Wordpress in subfolder and domain redirect

I installed a Wordpress website on an hosting space where there was already an active website. I didn't want to delete that so I put Wordpress in its own directory, having: http://domain.com/wordpress
In the wp-config.php file I set:
define('WP_HOME', 'http://domain.com/wordpress/');
define('WP_SITEURL', 'http://domain.com/wordpress/');
Then, I created an .htaccess file in the root and one in the wordpress folder. They respectively contain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ wordpress [L]
and
<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>
This way, when I go to http://domain.com/ I get redirected to http://domain.com/wordpress and everything on the website works fine. What I want, though, is to have the url displaying just http://domain.com, without the "wordpress" part. Is something I can achieve just with the .htaccess?
Another issue I'm facing is that there is a redirect domain set. There is this http://otherdomain.com pointing to http://domain.com. This other domain should actually be the main one. Right now, when I access http://otherdomain.com the website is displayed correctly; the links are working, but the url doesn't change accordingly: it always shows http://otherdomain.com, no matter what.
What I tried so far is to search and replace all the entries in the database, substituting the domains and modify other options accordingly. Doing this the website is shown without css, no images and no links working.
To fix issue with subfolder try to replace
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
with
RewriteEngine on
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteCond %{REQUEST_URI} !^/wordpress/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /wordpress/$1
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule ^(/)?$ wordpress/index.php
Don't forget to replace yourdomain.com with exact domain name.
Also, please revert database changes back, as right now WordPress is trying to find files in the root dir.
If this won't work, you might have such redirect option in your hosting account panel.

How can I fix this HTACCESS to allow Sub Folder Installation of WP (and it's own htaccess modrewrite rules)

I have a application that uses rewrite conditions, and then I added wordpress. I could not get /blog to rewrite properly.
Please see below for both HTACCESS files in root, and /blog (where wordpress is installed). I need to create a reference in root to allow /blog to work properly (im guessing). But how can I do this without breaking my other rewrite rules?
/.htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?sn=$1 [QSA,L]
#RewriteRule ^([a-zA-Z0-9/%-]+)$ index.php?sn=$1 [QSA]
Then in wordpress my wordpres install folder, i am simply doing:
/blog/.htaccess
#for blog directory only
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
... and all other rw conditions needed for caching, etc
The reason the error was persistent with this dual HTACCESS setup was because the /blog/.htaccess file was missing an additional line:
RewriteRule . /blog/index.php [L]
Once added, it operated fine. No chances needed to go into the root htaccess to reference the /blog/ rewrite conditions. This allows for a wordpress install in a blog folder, and above references dual HTACESS rules

Wordpress redirect to a subdirectory with permalinks

I have a wordpress blog in a subdirectory at domain.com/blog. I want to access it from the main domain, without anyone seeing it is in a subdirectory. I've managed to do a transparent redirect from the domain to the directory using some help, but I can't seem to get the pretty permalinks to work. This is what I use for the redirection:
RewriteEngine on
# Don't apply to URLs that go to existing files or folders.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Only apply to URLs that aren't already under /blog.
RewriteCond %{REQUEST_URI} !^/blog/
# Rewrite all those to insert /blog.
RewriteRule ^(.*)$ /blog/$1
# Redirect the root folder.
RewriteCond %{HTTP_HOST} ^(www.)?domain.com$
RewriteRule ^(/)?$ blog/ [L]
And this is what I would use for permalinks if the blog wasn't in a subdirectory and there wouldn't be any redirects:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
As far as I've tried combining the two rulesets, I either get an infinite loop or a not found from wordpress.
So how can these two rulesets be merged, so that the blog could be accessed by redirecting to a subdirectory and friendly urls could be used?
Thanks
perma link is just a setting in your admin side
go to setting and look for that and change the website url
not the wordpress url!!

Resources