Wordpress 404 issue with certain requests containg the string "admin-" - php

I have a wordpress page that generally works, but tonight, when updating the menu didnt work, I realized that something isn't working. So after choosing which menu items I want to add to the menu and hitting the button "Add to menu" the loading-circle starts to rotate endlesly. After inspecting more details in the developer-console, I see the following:
The GET request to https://URL/wp-admin/-ajax.php results in a 404. The wp-admin/-ajax.php doesnt look right to me. It should actually be wp-dmin/admin-ajax.php. So for some reason the admin part gets stripped away.
I notice the same symptom for loading some CSS files:
https://URL/wp-admin/-bar.min.js can also not be loaded. I think this should in fact be admin-bar.min.js.
My .htaccess looks as follows:
# 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 also already tried to disable all plugins to no avail.
Any hints on what could cause this issue? What can caus removing the admin part from these requests?
EDIT:
Could the following apache-config be the cause?
RedirectMatch permanent /admin(.*) https://konferenzimforum.at/wp-admin/$1

Why this happens in your configuration
The configuration line you included from your Apache config file is almost certainly the root cause of this behavior. To understand why this is, we must dive just a bit deeper into what the line actually does:
RedirectMatch permanent
Simply enough, this declares a permanent (HTTP code 301) redirect based on a RegExp pattern matching operation.
/admin(.*)
This pattern basically tells Apache, "match any URL that contains /admin, followed by any character, any number of times, while capturing the text after the /admin substring into a temporary variable, $1".
https://konferenzimforum.at/wp-admin/$1
This string then takes that earlier-captured value and instructs Apache to redirect the requestor to the URL https://konferenzimforum.at/wp-admin/, with the earlier-captured value appended to the end.
When this is applied to the URL in your example, wp-admin/admin-ajax.php would become wp-admin/-ajax.php, because /admin triggers the match, then uses the remainder of the URL captured in $1 as a post-fix, which then creates the non-existent URLs you're seeing in your browser's development tools. You can see a more visual & plain-English representation of how this pattern works in practice by using this Regex101. You'll notice on that page that the part that gets removed in your configuration is highlighted a different color than the part of the URL that gets passed through to the redirected URL.
How you can resolve it
There are a number of ways to resolve this; the easiest is probably to just remove this rule entirely (as it's not clear based solely on the information in the question why this rule would be necessary in the first place).
Another way to do it would be to ensure that only requests to /admin/* are matched, instead of arbitrarily matching all URLs that include a /admin sequence (which, as you've seen, can inadvertently target files beginning with /admin), which seems to be more of what you're trying to do (redirect requests for /admin to the appropriate /wp-admin resource):
RedirectMatch permanent /admin/(.*) https://konferenzimforum.at/wp-admin/$1

Related

htaccess RewriteEngine URL interpretation

Lately I have changed the way my website works - physical page for every article vs. dynamically loaded content without physical (sub)page, but I realized I cannot simply upload the new site files cos I would break up all the social platform sharing links, counters and stuff as there are literally thousands of the subpages.
I heard (I know about it) that via .htaccess and RewriteEngine (I need using RewriteEngine as all the code in htaccess is made for it) I can make pages load internally something completely different depending on the actual URL, like, for example, if I have actual URL link to one of my subpages:
http://sub.mypage.com/php/somearticle.php?j=en
...so without changing the text of the URL it would load my new site files internally on different principle, like this:
http://sub.mypage.com/?s=somearticle&j=en
Now I also need that those variables "j" and the "somearticle" to be dynamic, or better said they need to be copied exactly as they are from the physical URL in the addressbar (where "somearticle" is actually name of the originally physical php file and the "j" is just language variable) as it will be something else every time so I do not have to make thousands of lines in htaccess for every single concrete subpage - I need some universal code that would manage all the subpages (as the principle is the same for all, just php names changes and sometimes language = variable "j"), you see?
So can anyone help me telling me the exact syntax/code for this to achieve?
EDIT
So I was playing with it myself a bit and this seems to work if I set the subpage manually (NOTE: just a clarification - this is for my localhost:8081 therefore I have in place that 1st condition cos for server I have different version that has different path to index.php) + I slightly updated variable j part thanx to #Ben's post:
# LOAD PAGE DIFFERENT INTERNALLY - LOCALHOST
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteCond %{REQUEST_URI} /php/(.*)\.php$ [NC]
RewriteRule ^php/(.*).php$ /WWW/_PHP_/lego/index.php?s=$1 [QSA,L]
But unfortunately for some reason it affects every page on my site not only pages under /php/, so when I click to go to my first (default/initial = index.php) page it breaks it (it holds summary of all articles - they are not loaded) - anyone knows why, please?
SOLVED
So after small change to #Ben's code this is the right solution thus I take his solution as the right one (as it would actually work OK right away as it is if the page would be on server cos my test version is on my localhost where the path to /php/ directory is different))
LOAD PAGE DIFFERENT INTERNALLY - localhost
RewriteCond %{HTTP_HOST} ^localhost:8081 [NC]
RewriteCond %{REQUEST_URI} /php/([a-zA-Z0-9\-]+).php [NC]
RewriteRule ^ index.php?s=%1 [QSA,L]
REQUEST_URI is path component of the requested URI such as /php/somearticle.php, but not contains query string such as ?j=en.
The RewriteCond pattern, ! character (exclamation mark) is used to negate the result of the condition. To prefix with some pattern, use ^ character (caret), matches the beginning of a line.
%1 is the RewriteCond backreference that provides access to the grouped parts (in parentheses) of the pattern.
QSA flag, if the replacement URI contains a query string, the query string such as ?j=en will be appended to the newly rewrite uri.
RewriteCond %{REQUEST_URI} ^/php/([a-zA-Z0-9\-]+).php [NC]
RewriteRule ^ index.php?s=%1 [QSA,L]
See also: Apache Module mod_rewrite, RewriteRule Flags

how to change the URL from a GET-variable to imaginary directory

All the links in the website are a get-variable. The user opens always the index.php and gives different get-variables, which defines the different content.
Example:
The home page is example.com/?p=1
The contact page is example.com/?p=7
Now I want the URLs to look like example.com/contact. And because the number of pages is not static I can't create a directory for every page.
Probably I need a way to import the content of my index.php (example.com?p=3) to a path, which doesn't exist (example.com/new-path).
I've heard there is a way to solve that using the .htaccess file.
I'm not .htaccess / mod_rewrite expert, but just found this one may be useful for you. Note that you'll have to provide an entry for each page you are redirecting as the system will not know the relationships between the numbers and the pages by itself. You may have to fiddle around with it a bit to try and remove index.php.
# Original URL:
# http://www.example.com/index.php?p=1
# Desired destination URL:
# http://www.example.com/path-to-new-location/
# .htaccess syntax:
RewriteEngine on
RewriteCond %{QUERY_STRING} p=1
RewriteRule ^index\.php$ /path-to-new-location/? [L,R=301]
# Redirect URLs with query parameters (files placed in subdirectory)
Note, # is a sign for comments, anything on that line (in gray) will be ignored, it's sole purpose is to provide you information / comments on the code.
Source (line 52-63): Common .htaccess Redirects - Gist

htaccess redirect subdomains to certain page?

I need all subdomains to be redirected to a specific page, without actually changing the URL, because I will display different content on this specific page depending on what subdomain is in the URL.
Let's say my site is located at testdomain.com/site1/
I want all subdomains, like xyz.testdomain.com/site1/ or even xyz.testdomain.com to be redirected to a specific page at http://testdomain.com/site1/index.php/test.php
The browser will then need to be loading http://testdomain.com/site1/index.php/test.php, but the URL will still be xyz.testdomain.com.
The purpose of this is so that someone can go to abc.testdomain.com or xyz.testdomain.com and both will take the user to testdomain.com/site1/index.php/test.php, and then on test.php, I have some code that will grab the URL, and if the url is abc.testdomain.com, it will display certain content, whereas if the subdomain is xyz.testdomain.com it will display different content.
Is this something I can do in htaccess? If so, how?
Using mod_rewrite you can hack this together.
# Step 1: If the user went to example.com or www.example.com
# then we don't want to redirect them. (S=1 says skip the next rule)
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com
RewriteRule ^ - [S=1]
# Step 2: Anything else is redirected to our catcher script.
# Option 1: keeps the path they went to, but discards the domain
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/$1 [QSA,L]
# Or Option 2: take all requests to the same file
# i.e. xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php
RewriteRule ^ /var/www/cgi-bin/myfile.php [QSA,L]
QSA tells it to forward the query string, L tells it to stop looking for more redirects (not absolutely necessary but sometimes helps if you have a lot of this sort of thing going on).
You can also pass variables to your script as query parameters, and the QSA flag ensures they don't replace the original values;
# xyz.example.com/abc/def.txt => /var/www/cgi-bin/myfile.php?host=xyz.example.com&path=/abc/def.txt
RewriteRule ^/?(.*) /var/www/cgi-bin/myfile.php?host=%{HTTP_HOST}&path=/$1 [QSA,L]
It means you don't need to worry about figuring out where the request came from inside your script (which might actually be impossible, I'm not sure). Instead you can just read it as a normal parameter (it's hackable like a normal parameter too; be sure to sanitise it).

Ignoring/accepting forward slashes with htaccess and mod_rewrite

I need a little help on this one.
I'm in the process of making a php page that collects a query string, tests it against a database for matches, and then redirects the user to a different section of the site.
The is how it works currently (without htaccess/mod_rewrite):
User visits: domain.com/redirect/index.php?slug=Test_1
The php page sanitizes and looks up 'Test_1' in the database and retrieves a destination URL to redirect the user to. (e.g. domain.com/New_Test_1)
The php page then 301 redirects accordingly.
This part is working fine. However, due to some variables outside of my control, I need to interpret the original URL (using htaccess/mod_rewrite), like this:
domain.com/redirect/index.php/Test_1
Which still acts the same as:
domain.com/redirect/index.php?slug=Test_1
(note: yes, the index.php needs to stay in the url.)
I have this working with the following in my htaccess, but I know it could be better:
RewriteEngine On
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)$ %{DOCUMENT_ROOT}/redirect/index.php?slug=$2 [PT,L,QSA]
Part I need help with...
Some of the old url slugs had forward slashes in them, like this:
domain.com/redirect/index.php?slug=How_to_Code/Program
Without htaccess, the above still works, but fails with the pretty(ier) url:
domain.com/redirect/index.php/How_to_Code/Program
With my current htaccess, it only captures the 'How_to_Code' part, but ignores everything after it.
So my question is this: how can I restructure my htaccess to grab everything after domain.com/redirect/index.php/(.*)$, including forward slashes?
Edit: this .htaccess is going inside the /redirect directory
If you do this
RewriteRule ^redirect/index.php/([a-zA-Z0-9/_]+)$ redirect/index.php?slug=$1
That will accept forward slashes and underscores. To add anymore allowed characters just add them inside the [] square brackets. Certain characters may need to be escaped so just Google that.

help with URL rewrite for a multilanguage site with .htaccess (Apache)

I have a multilanguage site and I'm trying to rewrite the URL's with a fake directory something like this:
http://localhost/theSite/page.php?id=param&cat=param?lang=en,fr,es
to http://localhost/theSite/(en|fr|es)/page/param/param
.htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(fr|en|en)/(.*) $2.php?id=$1&cat=$2&lang=$3 [NL,QSA]
This resolves as a 404 error.
Any help will be apreciate.
RewriteRule ^(en|fr|es)/(.*?)/(.*?)/(.*) $2.php?id=$3&cat=$4&lang=$1 [NC,QSA]
I suppose you meant NC (no case), not NL. You referred to capture groups that didn't exist and repeated $2.
You're second capture will capture everything until the end of the URL. So it is possible you are doubling up on the extension or the wrong directory.
Although it shouldn't affect the redirect, you don't have a third capture, so where is $3?
Look at your headers and see where it is really redirecting to and comment back.

Categories