I had a rewrite rule on Apache for /year/month/date links in a form that specifically defined 4 digits, then 2 digits, then 2 digits, that looked like this:
^([0-9]{4})/([0-9]{2})/([0-9]{2})/$
On nginx this regex causes an error that says the whole line is not terminated by a ; sign, until i remove the {} brackets and leave the regex like this:
^/([0-9]+)/([0-9]+)/([0-9]+)/$
Is this limitation intentional on nginx's part or some mistake on my part?
The whole line from Apache:
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/$ index.php?page=date&year=$1&month=$2&day=$3
The whole (working) line from nginx:
rewrite ^/([0-9]+)/([0-9]+)/([0-9]+)/$ /index.php?page=date&year=$1&month=$2&day=$3;
If a regular expression includes the “}” or “;” characters, the whole expressions should be enclosed in single or double quotes.
http://nginx.org/en/docs/http/ngx_http_rewrite_module.html#rewrite
Related
I'm working on an nginx rewrite rule to redirect:
/collections/collection-name/products/product-handle-with-dashes
to:
/products/product-handle-with-dashes
I've got it almost working, the only issue I have right now if my rule to match the product handle is only returning the first string before the first hyphen.
My rule:
rewrite ^(/collections/.*)/products/(\w+)\.?.*$ /products/$2 permanent;
With this rule if I hit the following path: /collections/collection-name/products/some-product-handle it will redirect me to /products/some
what am I missing on my regex to allow it my second variable to capture the entire handle with dashes.
\w metacharacter of the PCRE/PCRE2 regex patterns include characters from ranges a-z, A-Z, 0-9 and underscore. It does not include a hyphen. You probably want [-\w] instead. The whole rewrite rule can be the
rewrite ^/collections/[^/]+(/products/[-\w]+)(\.\w+)?$ $1 permanent;
I have developed a project where url structure has changed from the old webpage and I need to create 301 redirects to avoid SEO penalization. After reading a lot I can't find how to do this rewrites.
Old URL
/es/madrid/comprar/893134/prop-712/
New URL
/es/property/prop-712/
Idea approach
RewriteRule ^/$1/property/$5 /$1/$2/$3/$4/$5/
What I need is using only the first path as param (/es/) and the last (/prop-712/) to restructure the URL /$first/property/$second and remove the $2, $3 & $4.
As you will see we share the last param (prop-712) of the URL. Any idea if this is possible?
Try the following at the top of the root .htaccess file using mod_rewrite:
RewriteRule ^([a-z]{2})(?:/[\w-]+){3}/([\w-]+?)/?$ https://example.com/$1/property/$2/ [R=301,L]
This will redirect a URL of the form /<lang>/<one>/<two>/<three>/<prop>/ (trailing slash optional) to https://example.com/<lang>/property/<prop>/. Where <lang> is any two lowercase letter language code and example.com is your canonical hostname. This matches exactly 3 path segments in the middle that are removed.
The regex [\w-]+ matches each path segment, including the property (last path segment). This matches characters in the range a-z, A-Z, 0-9, _ (underscore) and - (hyphen).
Only the first and last path segments are captured and later referenced using the $1 and $2 backreferences respectively. The parenthesised subpattern in the middle (ie. (?:/[\w-]+){3}) that matches the 3 inner path segments is non-capturing (indicated by the (?: prefix).
You do not need to repeat the RewriteEngine directive, since this already occurs later in the file inside the WordPress code block.
Test first with a 302 (temporary) redirect to prevent potential caching issues. Only change to a 301 (permanent) redirect when you are sure it is working as intended.
A quick look at your "idea":
RewriteRule ^/$1/property/$5 /$1/$2/$3/$4/$5/
In .htaccess files, the URL-path that the RewriteRule pattern matches against, does not start with a slash.
Backreferences of the form $n are used to reference capturing groups in the RewriteRule pattern (first argument). Backreferences can only be used in the substitution string (second argument)*1. You can't use backreferences in the RewriteRule pattern itself (which is a standard regex) - the $ carries special meaning here as an end-of-string anchor (regex syntax).
(*1 ...and the TestString (first) argument of any preceding RewriteCond directives, but this does not apply here.)
Reference:
https://httpd.apache.org/docs/2.4/rewrite/intro.html
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
I'm trying to write this redirection
images/catalog/1002/10002/main-200x250.12345.jpg to url images/catalog/1002/10002/main.jpg?w=200&h=250&vw=main
I tried this rule:
rewrite "^/images/(.*)/([a-z0-9]+)-([0-9])x([0-9]).([0-9]{5}).(jpg|jpeg|png|gif|ico)$" /images/$1/$2.$6?w=$3&h=$4&vw=$2 break;
It is not working, it return 404 not found error. I don't know what I'm missing.
Also when I remove double quotes (") I got this error
directive "rewrite" is not terminated by ";"
And I don't clear see the utility of the sign " and when should I use it or avoid it
I m working on a Mac with MAMP Pro v 5.2.2
You forgot to add a quantifier for the width and height numbers in your regex. Try this (I added a twice a +, you might want to use {X} instead, where X is the amount of digits for each number (if it is always the same amount of digits)):
rewrite "^/images/(.*)/([a-z0-9]+)-([0-9]+)x([0-9]+).([0-9]{5}).(jpg|jpeg|png|gif|ico)$" /images/$1/$2.$6?w=$3&h=$4&vw=$2 break;
Your reqular expression needs to be quoted because there is a } in it.
I think the nginx documentation about rewrite directive will answer your question, when a regular expression needs to be quoted:
If a regular expression includes the “}” or “;” characters, the whole
expressions should be enclosed in single or double quotes.
I have a valid RegEx pattern in .NET:
(?>.*param1=value1.*)(?<!.*param2=\d+.*) which matches if:
query string contains param1=value1
but does not contain param2= a number
It works in .NET. However IIS URLRewrite complains that it is not a valid pattern.
Can I not use zero-width negative look behind (?<! ) expressions with IIS URLRewrite?
Note that I tried to apply this pattern both in web.config (properly changing < and > to < and > respectively, as well as in the IIS Manager - all without success.
IIS URLRewrite default regex syntax is ECMAScript, that is not compatible with .NET regex syntax. See URL Rewrite Module Configuration Reference:
ECMAScript – Perl compatible (ECMAScript standard compliant) regular expression syntax. This is a default option for any rule.
You cannot use a lookbehind at all, you will have to rely on lookaheads only:
^(?!.*param2=\d).*param1=value1.*
Pattern explanation:
^ - start of string
(?!.*param2=\d) - if there is param2= followed with a digit (\d) after 0+ characters other than a newline fails the match (return no match)
.*param1=value1.* - match a whole line that contains param1=value1
You can enhance this rule by adding \b around param1=value1 to only match it as a whole word.
I have a RegEx problem. Consider the following URL:
http://ab.cdefgh.com/aa-BB/index.aspx
I need a regular expression that looks at "aa-BB" and, if it doesn't
match a number of specific values, say:
rr-GG
vv-VV
yy-YY
zz-ZZ
then the URL should redirect to some place. For example:
http://ab.cdefgh.com/notfound.aspx
In web.config I have urlrewrite rules. I need to know what
the regex would be between the tags.
<urlrewrites>
<rule>
<url>?</url>
<rewrite>http://ab.cdefgh.com/notfound.aspx</rewrite>
</rule>
</urlrewrites>
Assuming you don't care about the potential for the replacement pattern to be in the domain name or some other level of the directory structure, this should select on the pattern you're interested in:
http:\/\/ab\.cdefgh\.com\/(?:aa\-BB|rr\-GG|vv\-VV|yy\-YY|zz\-ZZ)\/index\.aspx
where the aa-BB, etc. patterns are simply "or"ed together using the | operator.
To further break this apart, all of the /, ., and - characters need to be escaped with a \ to prevent the regex from interpreting them as syntax. The (?: notation means to group the things being "or"ed without storing it in a backreference variable (this makes it more efficient if you don't care about retaining the value selected).
Here is a link to a demonstration (maybe this can help you play around with the regex here to get to exactly which character combinations you want)
http://rubular.com/r/UfB65UyYrj
Will this help?
^([a-z])\1-([A-Z])\2.*
It matches:
uu-FF/
aa-BB/
bb-CC/index
But not
aaBB
asdf
ba-BB
aA-BB
(Edit based on comment)
Just pipe delimit your desired urls inside of () and escaping special chars.
Eg.
^(xx-YY|yy-ZZ|aa-BB|goodStuff)/.*
But, I think you might actually want the following which matches anything other than the urls that you specify, so that all else goes to notfound.aspx:
^[^(xx-YY|yy-ZZ|aa-BB|goodStuff)]/.*
Assuming you want anything but xx-XX, yy-YY and zz-ZZ to redirect:
[^(xx\-XX)|(yy\-YY)|(zz\-ZZ)]