I am trying to redirect any calls to a specific file extension (here .jphp) to another url, using the filename part of the call as a subfolder. I try:
RewriteEngine On
RewriteRule [^/]/([^\.jphp])\.jphp /js/$1/
It should ignore anything before the last "/", then store the Filename in $1 and redirect to HOST/js//
What am i doing wrong?
A ^ as the first character in a regex character class [] is an inversion. [^/] evaluates to "any character that is NOT a slash", and the [^\.jphp] evalutes to "any charater that is NOT a ., j, p or h.
Since you're not specifying charater repetition limits, you're limiting your urls to be of the form
x/z.jphp
where x and z are any single characters which are NOT one of the excluded characters mentioned above.
Related
I use following RewriteRule syntax in apache and IBM HTTP servers:
RewriteRule ^/target$ www.something.com/content?param1=value1¶mN=valueN [R=301,L,NC,NE]
If a value contains % character, it does not work.
I tried dropping NE flag, no luck.
I tried encoding the % character itself as %25, no luck.
When the rewritten URL shows up on the browser, the % character is missing.
Can someone help with the correct RewriteRule syntax with an example?
Thank you very much for any help
Prefixing the % character with a backslash \ solved the problem.
This syntax corrupts the value of parameter p1 by dropping the % character:
RewriteRule ^/target$ www.something.com/content?p1=abx%xyc [R=301,L,NC,NE]
Prefixing % with a backslash preserves the value of parameter p1:
RewriteRule ^/target$ www.something.com/content?p1=abx\%xyc [R=301,L,NC,NE]
The % character causes the next two characters to be treated as the hex value of an ASCII character. This substitution alters the value of the parameter in the destination URL. Using the \ prefix prevents this substitution.
we have people coming to the website with trailing slash at the end of the URL e.g https//www.domain.com/page?name=john&name=doe/
for some reason, the logic to read query string parameters in code fails if there is a trailing slash at the end of the query string. Is there any way I can write a rule to check if there is trailing slash at the end of the query string them remove it.?
RedirectRule ^(http.+?\.com\/.+?\?.+?)\/$ $1
This should work, but not sure what rewrite regex format you need on iirf before entering to your page. I am using iirf.ini on my aspx C# project.
^...$ - asserts position at start and end of a line
.+? - match any char with quantifier one and unlimited, lazy
\. \/ \? - escape character for regular expressions
$1 - group 1 from the regex first bracket
I have a rewrite rule that is too strict.
RewriteRule ^folder/([^/\.]+)/?$ file.php?$1 [L]
This redirects http://www.domain.com/folder/$variable to http://www.domain.com/file.php?$variable HOWEVER.. if the variable has any dots or slashes it fails. I want it to redirect to http://www.domain.com/file.php? even if $variable is empty or contains any characters
Change ([^/\.]+) to (.*?).
The first matches at least one of anything except dots and forward slashes. The second matches 0 or more of anything, up to the posibility of a trailing slash (because of the /?$ at the end of your regex).
I read similar titles but I couldn't make it run..
Now, I have a code like this (originally ereg):
if (preg_match("[^0-9]",$qrcode_data_string)){
if (preg_match("[^0-9A-Z \$\*\%\+\-\.\/\:]",$qrcode_data_string)) {
I also tried using / at the beginning and end of rule but didn't work.
Any replies welcome.
With the preg_* functions you need delimiters around the pattern:
if (preg_match("#[^0-9]#", $qrcode_data_string)) {
# ^ ^
From the documentation:
When using the PCRE functions, it is required that the pattern is enclosed by delimiters. A delimiter can be any non-alphanumeric, non-backslash, non-whitespace character.
Often used delimiters are forward slashes (/), hash signs (#) and tildes (~).
I want to match urls in text and replace them with anchor tags, but I want to exclude some terminators just like how Twitter matches urls in tweets.
So far I've got this, but it's obviously not working too well.
(http[s]?\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?)
EDIT: Some example urls. In all cases below I only want to match "http://www.example.com"
http://www.example.com.
http://www.example.com:
"http://www.example.com"
http://www.example.com;
http://www.example.com!
[http://www.example.com]
{http://www.example.com}
http://www.example.com*
I looked into this very issue last year and developed a solution that you may want to look at - See: URL Linkification (HTTP/FTP) This link is a test page for the Javascript solution with many examples of difficult-to-linkify URLs.
My regex solution, written for both PHP and Javascript - (but could easily be translated to Ruby) is not simple (but neither is the problem as it turns out.) For more information I would recommend also reading:
The Problem With URLs by Jeff Atwood, and
An Improved Liberal, Accurate Regex Pattern for Matching URLs by John Gruber
The comments following Jeff's blog post are a must read if you want to do this right...
Ruby's URI module has a extract method that is used to parse out URLs from text. Parsing the returned values lets you piggyback on the heuristics in the module to extract the scheme and host information from a URL, avoiding reinventing the wheel.
text = '
http://www.example.com.
http://www.example.com:
"http://www.example.com"
http://www.example.com;
http://www.example.com!
[http://www.example.com]
{http://www.example.com}
http://www.example.com*
http://www.example.com/foo/bar?q=foobar
http://www.example.com:81
'
require 'uri'
puts URI::extract(text).map{ |u| uri = URI.parse(u); "#{ uri.scheme }://#{ uri.host[/(^.+?)\.?$/, 1] }" }
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
# >> http://www.example.com
The only gotcha, is that a period '.' is a legitimate character in a host name, so URI#host won't strip it. Those get caught in the map statement where the URL is rebuilt. Note that URI is stripping off the path and query information.
A pragmatic and easy understandable solution is:
regex = %r!"(https?://[-.\w]+\.\w{2,6})"!
Some notes:
With %r we can choose the start and end delimiter. In this case I used exclamation mark, since I want to use slash unescaped in the regex.
The optional quantifier (i.e. '?') binds only to the preceding expression, in this case 's'. There's no need to put the 's' in a character class [s]?. It's the same as s?.
Inside the character class [-.\w] we don't need to escape dash and dot in order to make them match dot and dash literally. Dash should be first, however, to not mean range.
\w matches [A-Za-z0-9_] in Ruby. It's not exactly the full definition of URL characters, but combined with dash and dot it may be enough for our needs.
Top domains are between 2 and 6 characters long, e.g. '.se' and '.travel'
I'm not sure what you mean by I want to exclude some terminators but this regex matches only the wanted one in your example.
We want to use the first capture group, e.g. like this:
if input =~ %r!"(https?://[-.\w]+.\w{2,6})"!
match = $~[1]
else
match = ""
end
What about this?
%r|https?://[-\w.]*\w|