We have a Wordpress multisite install with sites with each having its own domain:
fantastic-training
fantastic-thinking
perceptionprofiling
In the htdocs folder we have a sub-folder with assets we are using for one of the sites and that folder is called /Aj-Isle/ the non wordpress assets should be for the https://fantastic-training.com/ site.
However the folder is available to all 3 domains which is behaviour we don’t want. So trying to look at the file DOMAIN/Aj-Isle/south.png in all 3 domains works.
https://fantastic-training.com/Aj-Isle/south.png
https://fantastic-thinking.com/Aj-Isle/south.png
https://perceptionprofiling.com/Aj-Isle/south.png
Does anybody know how can we can use the .htaccess file in the htdocs folder to restrict it so that the Aj-Isle folder is only available when the https://fantastic-training.com/ domain is requested?
I think it has something to do with the HTTP_HOST variable?
Basically I know that in English the rules I want to create are these...
If you are inbound for /Aj-Isle/ and not requesting https://fantastic-training.com/Aj-Isle/ then redirect to the root of the target domain
If you are inbound for /limesurvey/ and not requesting https://perceceptionprofiling.com/limesurvey/ then redirect to the root of the target domain
Our .htaccess file in htdocs currently looks like this...
# BEGIN WordPress
<IfModule mod_rewrite.c>
AllowOverride All
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thanks in advance for any help or insights that can be offered.
Steve H-B
You can check the host name with a RewriteCond
RewriteCond %{HTTP_HOST} !fantastic-training.com
RewriteRule ^Aj-Isle / [R,L]
This checks, if the requested path starts with Aj-Isle. If it doesn't, proceed with the following rule(s).
If it does start with Aj-Isle, then the requested host is checked against fantastic-training.com. If the requested host is not fantastic-training.com, redirect the client to the root folder (/).
See RewriteCond for a detailed explanation
CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring:
You can prefix the pattern string with a '!' character (exclamation mark) to negate the result of the condition, no matter what kind of CondPattern is used.
Related
I'm working on a project made by a software engineer and by a team of non-technical people who know how to use Wordpress. That means that part of the platform is handmade, but still need to have Wordpress contents to be handled by non-software engineers.
My idea is to have two folders in my webserver root, one called /app/ containing the handmade code, and one called /wp/. So when the GitHub pipeline release new code into /app/ is sure not to touch stuff in /wp/ containing Wordpress.
I have achieved forcing the HTTPS with the following code:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
It works like a charm, users are successfully redirected if they use HTTP.
Now the problem is, I want to "reserve" the routes used by the handmade platform for myself, and in case the user is not calling any of those routes, then I pass the ball to Wordpress. I want it to appear as it is a single website, so I don't want the user to load the /app/appRoute or the /wp/wpRoute, I'd like to always load /route1, /route2, without specifying the subfolder into the URL.
The handmade platform should have priority, and it uses around 13 main routes (and some of them have subroutes), so I can hardcode them into the .htaccess file I guess. If the user is trying to load any of those routes, then I want to load the content into /app/route, if not, I'd load /wp/route. Of course Wordpress has its own .htaccess file, and the platform has its own, which is:
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
I'm very bad at working on .htaccess files and been searching around, it looks like I can't find the solution for my scenario.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
Should I have two .htaccess, write the 13 routes first, and then eventually redirect to the WP .htaccess (is it even possible?)?
Do I risk to make the user face a "Too many redirects" error?
This hybrid solution confuses me a lot. Does anyone who has been in the same situation has suggestions? Thank you in advance.
Given the following requirements:
/app subdirectory contains the "handmade code"
/wp subdirectory contains the WordPress site.
Neither /app or /wp should appear in the visible URL.
Should I have a single .htaccess file in the webserver root deleting the Wordpress one?
You could, but I wouldn't. Keep the WordPress .htaccess file in the /wp subdirectory. Everything WordPress is in the /wp subdirectory.
I would use 3 .htaccess files:
One in the document root. This manages the routing to either the "handmade code" in /app or /wp (WordPress). This should also manage the canonical redirects (ie. HTTP to HTTPS and www vs non-www)
One in the /app subdirectory that manages the routing within your "handmade code".
One in the /wp subdirectory that manages the routing within WordPress.
This allows you to keep the "handmade code" and WordPress entirely separate (in terms of development).
Your 3 .htaccess files would then look like this:
/.htaccess
# /.htaccess
RewriteEngine On
# HTTP to HTTPS redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite specific URLs to "/app" (handmade code)
RewriteRule ^app-route-1$ app/$0 [L]
RewriteRule ^app-route-2$ app/$0 [L]
etc.
# Rewrite everything else to WordPress
RewriteRule (.*) wp/$1 [L]
The "specific rewrites to /app" can be combined if there is a pattern. See below regarding static assets.
/app/.htaccess
# /app/.htaccess
RewriteEngine On
# Redirect any direct requests to "/app" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# Front-controller
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
/wp/.htaccess
# /wp/.htaccess
# Redirect any direct requests to "/wp" back to the root
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Note the RewriteBase directives and slash prefix on the substitution strings are specifically excluded, to avoid having to specify /app or /wp in the .htaccess file itself. (Although this might mess with WordPress, that likes to (unnecessarily) use RewriteBase and will try to overwrite the WP code block.)
You do not need to repeat the RewriteEngine directive, that already occurs later in the WP code block.
I don't know how you want to handle your static assets/resources (CSS, JS, images, etc.)? Currently, the above assumes that you will link directly to the assets within /app, ie. By including the /app path segment in the asset link. eg. <image src="/app/assets/images/myimage.png">. With WordPress you could link directly (ie. include /wp prefix) or omit /wp, since everything else is rewritten to /wp anyway.
Ideally, it would probably be preferable to omit both /app and /wp from your asset links, since you don't want to unnecessarily expose these to your users and it would otherwise make the sites dependent on these parent directories.
If your "handmade code" uses /assets for all the assets then you can rewrite these in the parent .htaccess file in the root, before your custom route rewrites:
# Rewrite "/app" assets
RewriteRule ^(assets)(?:/(.*)|$) app/$1/$2 [L]
This allows your "handmade code" to refer to assets using root-relative URLs, as if the app was installed in the document root.
This is what I have in my root folder, and when I go to my url, this is what is showing and not my website. It is a wordpress site, only structured a little different. (roots.io/composer)
This is the root folder: www.mysite.com (I know the site works, I tested it on another web hosting server.)
Inside the folder 'public_html' is the index.php file. And if I click it I go to the site, but it looks like this: /public_html
Only site directories are showing and I suspect I need a working .htaccess file.. Anyone?
/public_html
And so I have a hunch that I need the correct .htaccess file in order to find the correct folders and files. Or am I wrong?
Some other details:
In the browser I use a 'working-url': new.mysite.com because the main url is routed to another webhost until we get the new site up and running. But the files are inside the root folder. (Not mysite.com/new)
I've set the siteurl to: https://example.com/wp
And homeurl to: https://example.com
I've tried writing a .htaccess file like this, but getting the 500 internal error message:
#This is for subdirectory
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.mysite.com$
RewriteCond %{REQUEST_URO} !public_html/
RewriteRule (.*) /public_html/$1 [L]
#This is must
# 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've tried a lot of things, but can't seem to find the connecting dots. If someone out there with a clear head and more experience that could look into this, I would highly appreciated it.
Kind regards,
Me
I am setting up a very simple cache for my own WordPress plugin... I have been researching this here on StackOverflow and elsewhere for a couple of days. Found some great answers that were very close (looking for image files, etc), but none for this specific scenario - and modifying other solutions is not working.
I've got everything working except the rewrite rules to do the following:
Incoming request for either http / https
domain.com/page-slug/ OR domain.com/year/month/page-slug/
Need htaccess to first check the cache folder for
domain.com/wp-content/cache/myfolder/page-slug.html
(please note the incoming request can be for "/page-slug/" OR "/page-slug" and the cache file name has the ".html" extension to make it "page-slug.html")
If the cache file does not exist, then just
...go through the usual WordPress index.php process
Also -- what if there is no slug/page name in the url?
ie: the home page ... can htaccess direct
domain.com to domain.com/wp-content/cache/myfolder/index.html
and if the cached index.html does not exist, go to the WP index.php
Is there a way to do the above cache checks purely with the .htaccess rules? Any guidance would be greatly appreciated.
( I hope my explanation above was clear -- I get confused myself! LOL )
UPDATE:
I found a good answer in htaccess rewrite if file not exists in cache folder by anubhava and tried to modify it as follows, I think it is close, but its not quite working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# Admin area
RewriteRule ^admin(.*) admin$1 [L]
# Check if file *.* exist in the cache foldel
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/cache/ebg/$1 -f [NC]
RewriteRule ^(.+?)/?$ /cache/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
If the above solutions don't work, please contact your web host provider. Most likely you don't have Mod_Rewrite activated on your webhost package.
I have WordPress installed in the root folder and in a subfolder. I can access the home page for the WordPress site in the subdomain, but the permalinks does not work – error 404. I have tried to reset the permalinks, but it did not help. I can’t find any .htaccess file, so I have created one myself and placed it in the subfolder directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /projects/bigsargefitness/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /projects/bigsargefitness/index.php [L]
</IfModule>
Here is a link to the subfolder WordPress site:
http://ninahortendesigns.com/projects/bigsargefitness/
I have set the database options to the above direction.
Thanks for your help.
You will need to look at a few things:
.htaccess file in the root WP site (WP1) and edit it so that WP1 doesn't catch the URLs and generate 404 errors, I'm not sure if that comment assisted, I've used this answer for a similar issue.
.htaccess file in the sub WP site (WP2) and rename it to "htaccess.old" then log into your wp2/wp-admin, go to "Settings->Permalinks" check the URL structure is as desired (it doesn't usually change) and click "Save" at the bottom of the page. This will regenerate your .htaccess file within the context of the sub-directory and you shouldn't get 404 errors when you visit sub-pages like this
Here's the code from the first link edited so that it should work with your site, although if you have additional rules, you should only insert the line under the comment.
# BEGIN WordPress
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI} !(projects) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# END WordPress
If you have custom rules, only insert these lines
# Include in the next line all folders to exclude
RewriteCond %{REQUEST_URI} !(projects) [NC]
Assuming you're a designer and are uploading examples of sites you've built, you should only have to do step 2 the next time you upload a new site to the wp1/projects/ sub-directory
Cause
Error 404 occurs when the resource or path you requested cannot be reached. In this case it's likely because URL rewrite isn't working properly, hence the permalink requests were still being directed to the requested path (e.g. /projects/bigsargefitness/your-category/your-post-name) instead of the rewritten path (i.e. /projects/bigsargefitness/index.php).
The following solution assumes you use Debian/Ubuntu, otherwise the principles should remain the same.
Solution
Firstly, ensure that the rewrite engine has been installed:
sudo a2enmod rewrite
Then, ensure that the engine is allowed for the subdirectory by editing/adding the following lines in the file /etc/apache2/sites-available/your-site.conf:
<Directory /var/www/projects/bigsargefitness/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
The key here is to ensure that the line says AllowOverride All instead of AllowOverride None.
Finally, restart your server:
sudo service apache2 restart
Let me know if this helps.
I agreed to host a file for an online community, but I've since changed my site around so that it's now hosting a wordpress blog. What I'd like is to not break the existing URL to this one file, so, for example, when people navigate to the URL where file is being hosted, e.g. myblog.com/path/to/file, I'd like Apache to handle the URL rather than Wordpress, so that the file on the filesystem is delivered, rather than a Wordpress "We could not find the post" page. I think that the way to do this is to modify .htaccess so that that specific URL myblog.com/path/to/file does not execute index.php. Right now, here's what my .htaccess file looks like:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Could anyone tell me how to modify .htaccess to unmanage the URL to one file, but execute index.php for all other URLs?
Any guidance would be appreciated. Thanks.
These rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
will tell mod_rewrite to skip any file or directory which actually exist. As long as you don't move this special file of yours, then mod_rewrite won't redirect the rewrite to the main index.php.
Now, if you're moving the file elsewhere, but want to preserve the old url, then you'll have to do as toscho said in his answer do a Redirect.
Redirect permanent /old/path/to/file /new/path/to/file
# BEGIN WordPress ...