I'm having some problems with a mod_rewrite rule not matching, when I think it should.
The rule is as follows:
RewriteRule ^api/(.*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
And an example url is:
http://ourdomain.tld/api/aaaaa/bbbb/cccc%0Adddd.json
This rule works fine as long as you don't have $0A in the url (as far as I can tell).
I realise that . means anything other than a new line and have tried rules like the following to get around either new lines or percentage symbols as I'm not sure quite how it is formatted at the time that mod_rewrite sees it, but none have worked:
RewriteRule ^api/([.\s]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
RewriteRule ^api/([.\%]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
RewriteRule ^api/([.%]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
Strangely even the following fails on all urls, even those without % symbols:
RewriteRule ^api/([.]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
I'd be very grateful if someone could shed some light on this for me as I cannot really go changing how the API works (IE I cannot make it base64 encoded, etc) and need to get a value to it with a new line in it.
I've tested it at http://htaccess.madewithlove.be/ and their tool thinks it should work!
EDIT:
We had a solution for the URL above but it didn't work with an example like:
/api/something/sent+40.06K+bytes++received+7.83M+bytes++542.70K+bytes%2Fsec%0Atotal+size+is+19.83G++speedup+is+2520.16+%28Completed+in+14+seconds%29.json
Thanks.
....and need to get a value to it with a newline in it.
You're missing the caret ^ considered the negation operator inside of your character class [^.]
RewriteRule ^api/([^.]*)\.(json|xml|csv|txt|printr|basictxt)$ api/endpoint.php?api_request=$1&api_response_type=$2 [QSA]
See regex101 demo
Related
I am trying to write an .htaccess rule redirecting all URLs ending in Fizzle/xxxx_showabstract (where xxxx can be an arbitrary string). The rule I wrote is:
RewriteRule .*/Fizzle/(.*)_showabstract$ /abstractdisplay.php?journal=Fizzle&article=$1
But this, simple and straightforward though it to me, doesn't appear to be matching the intended URLs. What am I doing wrong? All help will be profoundly appreciated.
This may work.
RewriteRule ^.*/Fizzle/([^/]*)_showabstract$ /abstractdisplay.php?journal=Fizzle&article=$1 [L]
The issue you're having may be caused by a certain order on executing the regex. But if you use the ^ it will match a complete URL only.
I can't figure this one out no matter how many times I google it or think about it. I have a RewriteRule in my .htaccess file: RewriteRule ^/download/([^/\.])/?$ /downloadfile.php?f=$1 [L]
When I use this, my page loads fine, but going to the link http://www.example.com/download/file.ext, it pulls a 404 page. However, if I load the page, then change my RewriteRule to RewriteRule ^/download/([^/\])/?$ /downloadfile.php?f=$1 [L] (noting the RegEx change), the link works exactly how I expect it to... Until I reload the page, which results in a 500 error because of a bad regex expression? (I checked my Apache error log, thats how I know it reads as a bad regex)
So, what can I do to make this work? I've tried (.*) and ([.*]) for regex as well, that didn't work either.. can anyone tell me what I'm doing wrong?
mod_rewrite strips out the prefix (the leading slash) from the URI when you use it in an .htaccess file. Your regex needs to have it removed:
RewriteRule ^download/([^/\.]*)/?$ /downloadfile.php?f=$1 [L]
This should work:
RewriteRule ^/?download/([^/]*) /downloadfile.php?f=$1 [L]
The leading slash is required in apache 1.x and stripped in version 2.x
You were also denying the dot character.
I am working on a helicon rule and tried various combinations but they didn't work
I want the following URL to be resolved.
It can be this
www.test.com/myownpages/
or
www.test.com/myownpages
www.test.com/myownpages/?value1=test2&value2=test2
it should be resolved to
$1/test.aspx [NC]
If anyone gives something after myownpages, it shouldn't work
www.test.com/myownpages/test (This shouldn't work)
It tried the below so far
RewriteRule ^(.*)(\/\myownpages\/)(.*)(\?)?(.+)?$ $1/test.aspx [NC]
I am not very familiar with these rewrite rules, but maybe I can help with the regex. As I read it, you want to match any string ending with "/myownpages", "/myownpages/", or "/myownpages/?anything" and capture the part before that.
I'd use
^(.*)/myownpages(/([?].+)?)?$
to get this. See it in action at RegExr. If you need to escape the forward slashes, it becomes.
^(.*)\/myownpages(\/([?].+)?)?$
Note that this will not preserve the values in the query string; it will rewrite www.test.com/myownpages/?value1=test2&value2=test2 to www.test.com/test.aspx.
In case you want rewrite (NOT redirect) from /myownpages --> /myownpages/test.aspx, try using:
RewriteEngine on
RewriteBase /
RewriteRule myownpages/?$ /myownpages/test.aspx [NC,QSA,L]
QSA-flag appends the query string to the source path automatically.
I'm trying to understand why this regular expression isn't working in my .htaccess file. I want it so whenever a user goes to the job_wanted.php?jid=ID, they will be taken to job/ID.
What's wrong with this?
RewriteEngine On
RewriteCond %{QUERY_STRING} jid=([0-9]+)
RewriteRule ^job_wanted\.php?$ job/%1? [R]
I want it so when a user clicks on http://localhost/jobwehave.co.za/jobs/ID they are shown the same results as what below would show http://localhost/jobwehave.co.za/jobs?id=ID.
Sorry for the mix up. I still very confused to how this works.
The primary problem is that you can't match the query string as part of RewriteRule. You need to move that part into a RewriteCond statement.
RewriteEngine On
RewriteCond %{QUERY_STRING} jid=([0-9]+)
RewriteRule ^job_wanted\.php$ /job/%1?
Editing to reflect your updated question, which is the opposite of what I've shown here. For the reverse, to convert /job/123 into something your PHP script can consume, you'll want:
RewriteEngine On
RewriteRule ^/job/([0-9]+)$ /path/to/job_wanted.php?jid=$1
But you're probably going to have trouble putting this in an .htaccess file anywhere except the root, and maybe even there. If it works at the root, you'll likely need to strip the leading / from the RewriteRule I show here.
Second edit to reflect your comment: I think what you want is complicated, but this might work:
RewriteEngine On
RewriteRule ^/job/([0-9]+)$ /path/to/job_wanted.php?jid=$1 [L]
RewriteCond %{QUERY_STRING} jid=([0-9]+)
RewriteRule ^job_wanted\.php$ http://host.name/job/%1? [R]
Your fundamental problem is that you want to "fix" existing links, presumably out of your control. In order to change the URL in the browser address bar, you must redirect the browser. There is no other way to do it.
That's what the second cond+rule does: it matches incoming old URLs and redirects to your pretty URL format. This either needs to go in a VirtualHost configuration block or in the .htaccess file in the same directory as your PHP script.
The first rule does the opposite: it converts the pretty URL back into something that Apache can use, but it does so using an internal sub-request that hopefully will not trigger another round of rewriting. If it does, you have an infinite loop. If it works, this will invoke your PHP script with a query string parameter for the job ID and your page will work as it has all along. Note that because this rule assumes a different, probably non-existent file system path, it must go in a VirtualHost block or in the .htaccess file at your site root, i.e. a different location.
Spreading the configuration around different places sounds like a recipe for future problems to me and I don't recommend it. I think you'll be better off to change the links under your control to the pretty versions and not worry about other links.
The ^ anchors the regex at the beginning of the string.
RewriteRule matches the URI beginning with a / (unless it's in some per-directory configuration area).
Either prefix the / or remove the anchor ^ (depending on what you want to achieve)
You haven't captured the job ID in the regex, so you can't reference it in the rewritten URL. Something like this (not tested, caveat emptor, may cause gastric distress, etc.):
RewriteRule ^job/([0-9]+) job_wanted.php?jid=$1
See Start Rewriting for a tutorial on this.
You need to escape the ? and . marks if you want those to be literals.
^job_wanted\.php\?jid=9\?$
But although that explains why your pattern isn't matching, it doesn't address the issue of your URL rewriting. I'm also not sure why you want the ^ and $ are there, since that will prevent it from matching most URLs (e.g. http://www.yoursite.com/job_wanted.php?jid=9 won't work because it doesn't start with job_wanted.php).
I don't know htaccess well, so I can only address the regex portion of your question. In traditional regex syntax, you'd be looking for something like this:
s/job_wanted\.php\?jid=(\d*)/job\/$1/i
Hope that helps.
Did you try to escape special characters (like ?)?
The ? and . characters have a special meaning in regular expressions. You probably just need to escape them.
Also, you need to capture the jid value and use it in the rule.
Try to change your rules to this:
RewriteEngine On
RewriteRule ^job_wanted\.php\?jid=([0-9]+)$ /job/$1
Something like
ReWriteRule ^job\_wanted\.php\?jid\=([0-9-]+)$ /job/$1
should do the trick.
a short question about optional regex:
imagine a given page thats currently rewritten/beautified to:
/xx/somepage/description-description-description
while actually loading the ugly url below water:
/somepage?ln=xx
via this seemingly working rule:
RewriteRule ^([a-z][a-z])/(.*)/(.*?)$ /$2?ln=$1 [L]
However, currently the last portion /description is now compulsory. Without it error 404 occurs. What RewriteRule should I use to make the third and last part optional? Especially since it has no file-fetching meanings and is only for the user to make a url more descriptive.
In other words, I would like all three of the below to work the same way:
/xx/somepage
/xx/somepage/description-description-description
thanks very much
update
both of these seem to work fine:
RewriteRule ^([a-z][a-z])/(.*?)(/.*)?$ /$2?ln=$1 [L]
RewriteRule ^([a-z][a-z])/([^/]+)(/.*)?$ /$2?ln=$1 [L]
the difference being on the the middle part. I gather it has something to do with greedyness... but why/how they differ exactly... someone else might be able to shed light here.
Remove the final / in your rule, or place it in an optional group. As it stands, and from what I see, the description isn't completely necessary, but a trailing slash is.
New RegEx:
RewriteRule ^([a-z][a-z])/(.*?)(/.*)?$ /$2?ln=$1 [L]
You may need to ensure that the second pattern doesn't include a slash:
RewriteRule ^([a-z][a-z])/([^/]+)(/.*)?$ /$2?ln=$1 [L]