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.
Related
i try to catch multiple slashes at the ende or inside of url, but not those slashes (which could be two or more), which are placed after protocol (http://, https://, ftp:// file:///)
i tried many findings in similar SO-threads, like ^(.*)//+(.*)$ or [^:](\/{2,}), or ^(.*?)(/{2,})(.*)$ in http://www.regexr.com/, https://regex101.com and http://www.regextester.com/. But nothing worked clear for me.
Its pretty weird, that i can't find a working example - this goal isn't such rar. Could somebody share a working regex?
Here is a rule that you can use in your site root .htaccess to strip out multiple slashes anywhere from input URLs:
RewriteEngine On
RewriteCond %{THE_REQUEST} //
RewriteRule ^.*$ /$0 [R=301,L,NE]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1.
Pattern inside RewriteRule automatically converts multiple slashes into single one.
Im trying to redirect a bunch of support files to a different support system except for any files that have the folder software_updates in them. the following is the rule that I wrote.
RewriteRule ^/support/.*(?!software_updates).*$ newurl_location [NC,L,R=301]
this excludes /support/software_updates/ but not /support/product/software_updates Im trying to exclude any URL that has software_updates anywhere in the URL after support.
Try:
RewriteRule ^/support/(?!.*software_updates) newurl_location [NC,L,R=301]
I don't have Apache handy to test it, but it should work.
I have tested this and believe that it is what you're looking for (please note the changes in the / and the .* )
RewriteRule ^support/(?!.*?software_updates) newurl_location [NC,L,R=301]
You were nearly there.
First off, you don't need the initial /
You don't want .*(?!software_updates).*, because this will match software_updates. Why? The dot-star eats the whole string, then, at the end, you have the assertion that software_updates is not next. And of course this is true.
This should do the trick.
RewriteRule ^support/(?!.*software_updates).*$ newurl_location [NC,L,R=301]
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
I have done some research so far, but could not find the solution. I have a client who has this old file structure on the URL:
/index.php?process=views/article.php&articleId=44102
which should redirect to this URL:
/news/title-of-article/
I found that I may need this in my htacces file:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/ [R=301,L]
and when I test it with the full URL, it won't redirect. But when I leave out the forward slash like this:
/index.php?process=viewsarticle.php&articleId=44102
it redirects fine. So I assume the forward slash is the problem here.
I'm not a specialist in setting up .htaccess files and this drives me crazy, because it seems that I have the right solution in plain sight, but don't know why the forward slash is preventing the redirect.
If you can't answer with the final solution, I'd appreciate to have some kind of tool (website, software, etc) where I would be able to test these rewrite conditions in detail. Maybe there is something like the regex-testers that are available for regular expressions, but just for RewriteCond and RewriteRules that I can use to figure it out myself.
Thanks in advance
Marian
Problem is that you're not stripping out QUERY_STRING In rewritten URI and because of that resulting URL also contains QUERY_STRING as process=views/article.php&articleId=44102 and it matches rule again causing redirection loop.
Change your rule to this:
RewriteCond %{QUERY_STRING} &articleId=44102 [NC]
RewriteRule .* /news/title-of-article/? [R=301,L]
? in the end will strip out existing query string.
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.