.htaccess file Wordpress RewriteRule Block Rearrange to the top - wordpress

I was having issues (401 error) trying to reach my website via rest API. I moved the Wordpress block in my .htaccess file to the top and now my website is reachable. This is the first block that I now have in my .htaccess file?
# BEGIN WordPress
<IfModule mod_rewrite.c>
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]
Do I need to combine the two lines of the RewriteRule lines like this? Does it change anything functionally?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Prior to me making any changes, the first block in the .htaccess file was:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
So by moving the "Wordpress block" it just adds the one line RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

Do I need to combine the two lines
You certainly don't "need" to combine those two lines.
It will probably make no difference to accessing your rest API if you do.
A single, combined directive
By combining those two directives, as you have done, will mean the HTTP_AUTHORIZATION env var (used to help with HTTP Authentication) will only be set on requests that are routed through WordPress (including the homepage). And (on Apache*1) the REDIRECT_HTTP_AUTHORIZATION env var will not be generated when requests are rewritten to the WordPress front-controller (index.php), ie. page requests other than the homepage. Although I'm pretty sure that WordPress does not take advantage of this anyway. The HTTP_AUTHORIZATION env var will not be set on requests to static resources (images, CSS, JS, etc.) as it would have been before.
Two separate directives
With the two separate directives, as in the orginal code, the HTTP_AUTHORIZATION env var is set on every request, including requests for static resources. (But ordinarily, this is redundant.) And (on Apache*1) the REDIRECT_HTTP_AUTHORIZATION env var will be generated when requests are rewritten to the WordPress front-controller (index.php), ie. page requests other than the homepage.
(*1 As opposed to LiteSpeed, where env vars of the form REDIRECT_... are not generated anyway.)
I moved the Wordpress block in my .htaccess file to the top and now my website is reachable.
That isn't necessarily the correct thing to do. Ordinarily, the WordPress code block (the front-controller) should appear later in the file. There may have just been a single conflicting rule that should have been "modified" or perhaps moved to after the WordPress code block. But moving those directives to after the WordPress code block may effectively "disable" those rules entirely.
UPDATE:
Prior to me moving the "Wordpress" block to the top, this is what the
first block had:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
That code block is just an erroneous "copy" of the WordPress code block (less the HTTP_AUTHORIZATION var) and should be deleted altogether!

Related

Wordpress at root and vue in sub dir. Masking of vue with wordpress URL. URL points root and page load is of vue

At root there is WordPress site. And in subdirectory calles 'vue' there is vue build is there. In wordpress URL if 'podcaster' or 'crowd' is not a part of a URL then I want to load vue page without changing browser URL.
Below is my folder structure
Target is:
www.example.com/podcaster //display in the address bar, points to WP (var/www/html/)
www.example.com/crowd //display in the address bar, points to WP (var/www/html/)
www.example.com/username //display in the address bar, points to VUE (var/www/html/vue)
www.example.com //display in the address bar, points to WP (var/www/html/)
Below is the WordPress root directory .htaccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Rewrite rule for vue
RewriteCond %{REQUEST_URI} !^/crowd/
RewriteCond %{REQUEST_URI} !^/podcaster/
RewriteRule (.+) /vue/$1 [L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Below is vue sub directory .htaccess:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
You've not stated exactly what the problem is, however...
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteRule ^(.*)$ /vue/$1 [L,NC]
This will result in requests for the root being internally rewritten to the /vue subdirectory (I'm assuming you've placed this at the top of the WordPress .htaccess file in the root directory). You've stated that WordPress should be served from the root. In which case you should change the RewriteRule pattern from .* (0 or more) to .+ (1 or more) to avoid being triggered for requests to the root (base URL / homepage).
You will also need to ensure that rewritten requests (by the WordPress front-controller - to index.php - in the code that follows) are not also rewritten (otherwise everything will ultimately be rewritten to the /vue subdirectory). We can do this by adding another condition that checks against the REDIRECT_STATUS environment variable, which is empty on the initial request and set to 200 (as in 200 OK status) after the later rewrite.
UPDATE: And I suspect you also have a number of static resources (CSS, JS, images, etc) that also need to be excluded, so we probably need to add a filesystem check for those, so the request is not rewritten if it already maps to a file (or directory).
For example, bringing the above points together, this becomes:
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} !/podcaster/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.+) /vue/$1 [L]
I've removed the NC flag as that was superfluous here. Note also that the condition is successful when the string "/podcaster/" (note that slashes) does not appear anywhere in the URL-path. Should this not be restricted to the start of the URL-path perhaps? eg. !^/podcaster/.
Also, where is the rest of the WordPress .htaccess file? The WordPress front-controller (the part that normally appears after the # BEGIN WordPress comment marker) should appear after your custom directive. If you place your custom rewrite at the end of the WordPress .htaccess file, after the WP front-controller section then your directive will not doing anything since all requests will be routed to WordPress.
Note that you should place your custom directives before the # BEGIN WordPress comment marker. You should never edit the code between the # BEGIN WordPress and # END WordPress comments since this block of code is maintained by WordPress and your code could be overwritten when WP updates (unless you take additional steps to prevent this). There is no need to repeat the RewriteEngine or <IfModule> directives.
Below is vue .htaccess which in "vue" sub directory
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vue/
RewriteRule ^vue/index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vue/index.html [L]
</ifModule>
The Vue .htaccess file isn't quite right (although should work OK). The first RewriteRule directive (which is simply an optimisation) should not include the vue subdirectory in the regex. In other words, it should be written:
RewriteRule ^index\.html$ - [L]
Since the URL-path that is matched is relative to the filesystem path that contains the .htaccess file.
In fact, you can remove all instances of vue from this file (which makes it simpler and more portable), providing you also remove the RewriteBase directive entirely. For example:
# /vue/.htaccess
RewriteEngine On
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
Relative substitution strings (ie. index.html in the 2nd RewriteRule directive above) are relative to the directory that contains the .htaccess file (providing you don't have a RewriteBase directive that states otherwise). So, the above naturally rewrites the request to /vue/index.html without having to explicitly state the directory.

.htaccess to serve specific pages with prefix path in front of it

I need to serve these pages:
/category/*
As
/shop/category/*
I need the url to have /shop in front of /category/ and it needs to handle all nested path structures within category also.
How can I do this via .htaccess preferred?
There is also a /shop/ page that I do not want disturbed by any .htaccess edits.
So, I just need to serve /category/ pages with the url having /shop/category/ instead, is this possible via .htaccess?
So I'm a beginner here in .htaccess rewrites, but have tried this:
RewriteCond %{HTTP_HOST} ^category/$
RewriteRule ^shop/category/(.*)$ category/$1 [L]
It doesn't do anything tho...
Also tried this:
RewriteCond %{HTTP_HOST} ^category/(.*)
RewriteRule ^$ /shop/category/$1? [R,L]
Again, no effect on anything.
I am using Wordpress, so it seems it has some power over my rewrite rule perhaps?? Anyways, here is my entire .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
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
Have tried the following within the init wordpress action also:
add_rewrite_rule('^/shop/category(?:/.*)?', '/category/$matches[1]', 'top');
No luck on this.
Ok, seems that I might be getting somewhere here...
The following add_rewrite_rule below matches only the first category, but doesn't match for child categories:
add_rewrite_rule('shop/category/?([^/]*)', 'index.php?product_category=$matches[1]', 'top');
The custom taxonomy that I am using is product_category, not wordpress default category since I don't want my product categories mixed with post categories. So, I just need to figure out now, how to match all paths (which include child categories), for example:
/category/gelest-inc/silanes-silicones/
silanes-silicones is a child of gelest-inc, how to capture this in the rewrite rule above also?
If I understand your question correctly following .htaccess should serve your requirements:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^category(?:/.*)?$ /shop/$0 [R=301,NC,NE,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Your rule says if HTTP_HOST is catagory/ then rewritehomepage (^$ matches an empty uri eg : /) to /shop/catagory. Your RewriteCond never meets as you are matching against HTTP_HOST header using URI in pattern.
If you want to redirect /catagory/ to /shop/catagory , you can use
RewriteEngine on
RewriteRule ^catagory/([^/]*)/?$ /shop/catagory/$1 [L,R]
You have the WordPress rules, and after the rules comes the "shop" rule.
When the shop URI is not a real file or directory, requests for it will already be handled by the WordPress rules and rewritten to index.php. Apache then iterates a second time with index.php, but it doesn't match the shop rule and never redirects or rewrites.
To make this work the shop rule must come first
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Now it depends on what you want to achieve, redirect from
RewriteRule ^shop/category/(.*)$ category/$1 [R,L]
or to shop/category
RewriteRule ^category/(.*)$ shop/category/$1 [R,L]

How to ignore sub-folder requests and continue processing URI mapping using mod_rewrite?

An default example file for a default Wordpress deployment 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>
I have a special case scenario though where I have a subfolder as such in my root web directory that I need ignored:
/folderA/folderB/specialCase.php
I basically need:
/folderA/folderB/*
to be ignored, but I do need:
/folderA/folderB/specialCase.php
to be directly accessible and served.
The problem is that /folderA/folderB currently maps to a URI in my Wordpress install. Since the directories actually exist in the web root folder, currently Apache is serving up the index.php within /folderA/folderB/.
I need it to skip over these subfolders and continue processing URI mappings as usual, except in the special case where specialCase.php is requested?
Difficult? Hard? I'm stumped at the moment. Have been experimenting with the [PT] (passthrough) flags but no luck so far.
Thanks!
Couldn't you just check for that specific file, and serve it. Otherwise redirect the rest to the wordpress rules. Maybe something like this.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/folderA/folderB/specialCase\.php$ [NC]
RewriteRule ^ - [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} ^/folderA/folderB/?$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Also you could probably add another .htaccess in folderA/folderB/.htaccess with this
DirectoryIndex disabled
Which will also prevent it from serving index.php in that directory only.

Rewrite index and one folder to static pages with WordPress rewrites on the other

I'm looking to have all WordPress rewrites to work except:
Home page as a static html page, content is in index.html in the root folder
/business be routed to a business folder in the root dir which contains static html pages.
When I set the index to . /index.html [L] none of the regular wordpress rewrites work. However if I set DirectoryIndex /index.html I can't figure out how to get rewrites to work for /business which contains HTML files that need to be served up on the http://mywebsite.com/business url. Full rewrite rules:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule . /index.php [L]
RewriteRule . /index.html [L]
Any help is appreciated and, more than just an answer, an explanation of what each portion of the lines you provide does may help myself and others understand how to take this on on their own next time.
UPDATE: Rules are still not working. /wp-admin has worked all along though.
Try to stick with default WordPress rewrites and simply add DirectoryIndex to prefer .html files over .php files:
DirectoryIndex index.html index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
So rewrites will only happen when requested files or directories are missing (RewriteConds with !-f and !-d flags). And if there is a static index.html along with index.php in a directory (root directory for example), it will be served first.
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryindex
Try:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteRule ^business/ - [L]
RewriteRule ^index\.php$ - [L]
RewriteRule ^$ /index.html [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Two issues need to be solved:
Serve a particular HTML file whenever someone needs the front page.
Serve static files from a static directory alongside WordPress.
To solve the first issue, I would rename index.html to front-page.php and move it inside my current theme folder. Then WordPress would serve it (or rather: use it as a template) whenever someone requests the front page, according to the Template Hierarchy.
There is a cost to this solution compared to actually serving a static file: Whenever someone requests you front page, WordPress will still be loaded. If the goal of serving your front page statically is to save server resources by not loading WordPress or PHP on every page load, you should look into caching plugins.
The second issue should not be an issue at all, because the standard WordPress rewrite rules already allow for static files and directories. The lines
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
mean that the redirect to WordPress won't happen if the URL requested is an actual file (-f) or directory (-d). Therefore you will not need any extra rewrite rules to access the /business directory.
The standard WordPress rewrite rules are explained below:
RewriteEngine On # Turn on the rewrite engine
RewriteBase / # Use the domain root as the base of rewrites
RewriteRule ^index\.php$ - [L] # If someone requests the WordPress entry point, stop here
RewriteCond %{REQUEST_FILENAME} !-f # If the requested URL is not an actual file ...
RewriteCond %{REQUEST_FILENAME} !-d # ... and if the requested URL is not an actual directory ...
RewriteRule . /index.php [L] # ... then rewrite it to the main WordPress entry point
When /index.php is loaded, this file will then in turn load WordPress and everything that comes with it.
Replace this code with your HTACCESS file in root
# Switch rewrite engine off in case this was installed under HostPay.
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 53
DirectoryIndex index.cgi index.php
# 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
And also change the URL's from database...

RewriteRule exception for static page outside WordPress

I'm searching Google since at least 20 minutes and I can't believe it's that hard to find...
It should be pretty easy but I can't figure it out...
THE PROBLEM:
I want to put a HTML file at the same level of my Wordpress, but don't want the RewriteRules to "trap" this page and think it's a redirect...
THE HTACCESS:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# >> here << this next line is my TEST....
RewriteRule ^test\.htm$ test.htm [L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Of course, a page called "test.htm" is uploaded on the server.
As stated, the line where I put my test doesn't work. It shows the WordPress homepage, instead of my "test.htm" page I uploaded.
You might try this one:
RewriteRule ^test\.htm$ - [PT,L]
It passes through (PT) the request and does not apply any further rules (L).

Resources