This one allows everything with .html extension that contains no slashes:
rewrite ^/([^/]+).html$ ...
I need to add another catch to it: URL must contain at least one dash, then it can be rewritten.
How to do that?
Just use logic. Word with at least one dash could be expressed as two words with dash between them. So solution is simple:
rewrite ^/([^/]+-[^/]+)\.html$.
Also you forgot to escape dot (.) so your regexp also match url /somesstrangehtml
Related
I'm currently trying to get a WordPress site (using the Redirection plugin) to always add a trailing slash to any URL without one, but only if the URL doesn't end in a slash already or a file extension (so images, .php files/pages, etc. aren't affected).
e.g. www.mysite.com/page becomes www.mysite.com/page/, but www.mysite.com/page/ and www.mysite.com/file.php are left alone.
I was able to get the first half working (forcing a trailing slash if it doesn't already end in one), but I'm struggling to add the extra condition.
This is what I currently have:
Source URL: /([^\/]+)$
Target URL: /$1/
Using .htaccess, etc. isn't an option unfortunately. Any advice would be greatly appreciated.
If there can not be a dot in the part after the last / then you can add it to the negated character class.
If the delimiter is not a / then you don't have to escape it in the pattern.
/([^/.]+)$
Regex demo
Depending on what you need, you can add (?!.*\.) (not followed by a period), or (?!.*\.php$) (not followed by a php extension), or (?!.*\.(?:php|jpg)$) (not followed by a php or jpg extension), ecc.
Full examples:
\/(?!.*\.)[^\/]+$
\/(?!.*\.php$)[^\/]+$
\/(?!.*\.(?:php|jpg)$)[^\/]+$
In these examples the matching group is not necessary, so you can replace with $0\/.
See working demos here and here.
I am trying to use Nginx rewrite static file path to strip the hash added for cache busting. The hash is always 10-symbol long. For example,
/min/3rd.party.min.1234567899.js has to become /min/3rd.party.min.js
I have tried this, but it doesn't work (fails at configtest) and also looks way to complicated.
location /min/ {
root /opt/app/public;
rewrite ^.*(?<=(.))[a-z0-9]{10}[.](?=(js|css))[js|css]$ $1$3;
}
I have no idea how you arrived at your regular expression pattern, but the following seems to work:
rewrite "^(.*)\.\w{10}\.(js|css)$" $1.$2 break;
Any pattern that contains a brace, must be placed within quotes. Use the break suffix to process the rewritten URI within the same location. See this document for details, and this useful resource on regular expressions.
I've searched through all of the related topics here but none seems to answer my specific need. Here is the problem: Given a URL (sans protocol), I want to extract the subdomain portion, excluding www. The domain portion is always the same so I don't need to support all TLDs. Examples:
www.subdomain.domain.com should match subdomain
www.domain.com should match nothing
domain.com should match nothing
This is one of the many iterations I have tried:
[^(www\.)]\w+[^(\.domain\.com)]
Square brackets indicate character class and will remove all the order of otherwise special meaning of most characters.
You can try something like this instead:
((?:[^.](?<!www))+)\.domain\.com
regex101 demo
To return what you're looking for instead of retrieving it through submatches:
((?:[^.](?<!www))+)(?=\.domain\.com)
regexp101 revised
i am trying to rewrite any URL that match this pattern:
~/Ahmed
~/Name
to this:
~/User/Ahmed/Ahmed.aspx
~/User/Name/Name.aspx
and i can write them individually but what i am trying to do is detect any URL that look like "~/User/Ahmed/Ahmed" and auto rewrite them to this "Ahmed"
thanks
Hopefully you're using the UrlRewritingNet library, not UrlRewriter? The former is suggested over the latter.
However, in either you can use a regex:
"~/User/([^/\\]+)/\1.aspx" -> "~/$1" //For ".aspx" in the URL
"~/([A-Za-z]+)" to "~/User/$1/$1.aspx" //For /Name in the URL.
Note the ([^/\]+) means any set of characters without slashes,
and "\1" is a backreference to the previous capture, that ensures the name is an exact duplicate. Note that you should enable "ignore case" if you want to support "/User/ahmed/Ahmed.aspx" and not just "/User/Ahmed/Ahmed.aspx".
I am using the Intelligencia URL rewriter in my asp.net web application.
I use the web.config mappings
I'm trying to map the following url:
www.mydomain.com/product-deals/manufacturer-model_PRODUCTId.aspx
To:
www.mydomain.com/ProductInfo.aspx?productID=xxx
obviously in the above example, xxx is replaced from the "productId" from the "friendly" url.
In my web.config, I've got so far:
<rewrite url="~/contract-deals/([\w-_]+)/_(.+).aspx" to="~/ProductInfo.aspx?productId=$1"/>
This isn't working however.
I need the correct regex to use for my requirements (regex really isn't my strong point!!)
One problem is that you have product-deals in your sample and contract-deals in the regex.
Next, your regex has an extra slash, and you don't escape the dot (though it can match a dot anyway). Also, $1 refers to the first capturing group, which in your case is "manufacturer-model".
This regex should get you what you want:
product-deals/[\w_-]+_(.+)\.aspx