I am having a bit of a problem with .htaccess and numbers.
I have a directory
http://somedomain.com/directory/
When I add a number to it, I want to use htaccess to create a variable.
http://somedomain.com/directory/
RewriteRule ^[0-9]+$ index.php?action=viewclient&clientnumber=$1 [L]
When I put this URL, I want the clientnumber to be created
http://somedomain.com/directory/5
However, when I put no number in it, it still creates the $action variable with a blank $clientnumber.
http://somedomain.com/directory/
Is there a change I need to make to the htaccess to only add these variables if there is a number in the URL but blank. To skip this htaccess line if no variable is added.
Thankyou
EDIT: i cant get the URL's to work. from the answers provided. i'm off to bed & will re-check this over the weekend.
if I put /directory/ I get what i want. if i put /directory/4 or directory/87 (any number) i get a 404 error.
EDIT2 - WEIRD ???
I'm off to bed, but decided to try again & selected a different client. the URLs are the SAME format.
some clients work (display the correct page), other URL's (CORRECT URL), show a 404 page.
I'll try & debug more in the weekend. - very weird. (I'll add a debug script / log at the top of my index.php - to see if the page is even loading.)
EDIT 3:- I've added a small log script at the top of index.php - it DOES record the valid / working URLs, but does not activate if its a 404 error (the error is the SAME format URL - so should activate index.php - & then my index.php should do the calcualtions / checks to see if the values are valid. but its not even getting to my php script.
OK, i'm off to bed - will retry in the weekend.
RewriteRule ^[0-9]+$ index.php?action=viewclient&clientnumber=$1 [L]
$1 refers to the first capture group. You don't have any capture groups in your regular expression. This is correct:
RewriteRule ^([0-9]+)$ index.php?action=viewclient&clientnumber=$1 [L]
Changing it to
RewriteRule ^([0-9])$ index.php?action=viewclient&clientnumber=$1 [L]
(no + after [0-9] )
will mean the rule is only executed if the number 0-9 is present in the request
Related
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
I am trying to use a redirect to the same page but with a query string.
RewriteRule ^fixtures(.*)$ views/fixtures.php
RewriteRule ^fixtures/(.*).([a-zA-Z_-]*) views/fixtures.php?date=$1
When I click any links from the page, nothing happens. In chrome debugger it cancels the request.
Any idea what I am doing wrong?
Cheers
It would be great to see a working example but just looking at your rewrite rules it seems there might be a small issue with your regex.
The issue I can see is that your first line expects something like:
example.com/fixtures*absolutelyanythingeleseontheurl*
But I think this is being overidden by the next line which is capturing two sections of the url, anything after fixtures/ and anything after the fullpoint that is a-zA-Z, _ or -. But your initial .* has already taken care of that.
RewriteRule ^fixtures/(.*).([a-zA-Z_-]*) views/fixtures.php?date=$1
Have you tried something like this:
RewriteRule ^fixtures$ views/fixtures.php
RewriteRule ^fixtures/(.*) views/fixtures.php?date=$1
The above would do the following:
example.com/fixtures/ will resolve to views/fixtures.php
and the next line would resolve to the following:
e.g. example.com/fixtures/22-06-2014 to views/fixtures.php?date=22-06-2014
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).
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.
So I have errors which are passed by the url, for example
index.php?error=nojs
will then be parsed by PHP to return an error message, for example: Please enable Javascript
I'm using the following line in my .htaccess to make the url easier to manage
RewriteRule ^ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
It makes my URL look like this:
site.com/ERROR_nojs
The problem is, this only works for the root,
index.php?error=nojs works fine however
test/index.php?error=nojs does not?
So how can I convert the variable for every directory?
Thank you. (My original script handles hundreds of errors and filters out ones that might be useful to output to the user. It would be stupid to redirect them to the index just so they can see a small with an error message in it)
EDIT:
as Shai Mishali pointed out removing the '^' before ERROR did the trick.
RewriteRule ERROR_(.*)$ index.php/?error=$1&%{QUERY_STRING} [L]
But I forgot to tell you I have another variable ?page=
I need get that vairbale and add it to the url in order for this to work..
e.g:
index.php?page=home&error=nojs
= site.com/home/ERROR_nojs
so
www.site.com/?page=home&error=nojs = www.site.com/home/ERROR_nojs
or
www.site/?page=about&error=unknown = www.site.com/about/ERROR_unknown
I'm pretty sure your problem is your rule is looking for something that starts with ERROR (the ^ sign) .
/ERROR starts with error, which works in your root , but
/tests/ERROR starts with tests , so it won't recognize it.
Try removing the ^ sign and see what happens.
Shai.
You can use below code
RewriteRule ^([a-z0-9A-Z]+)/([a-z0-9A-Z]+)$ ./index.php?page=$1&error=ERROR_$2 [NC]
Your URL site.com/home/ERROR_nojs will treated as index.php?page=home&error=nojs and you can get the values by GET method. For more info