I have 7 urls:
url: /v/3b89441db7986135e5eb9e1debf0cc23
url: /v/3b89441db7986135e5eb9e1debf0cc23/login
url: /v/3b89441db7986135e5eb9e1debf0cc23/logout
url: /v/3b89441db7986135e5eb9e1debf0cc23/access
url: /v/3b89441db7986135e5eb9e1debf0cc23/delete-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
url: /v/3b89441db7986135e5eb9e1debf0cc23/finalise
How could I write a rewrite rule that if these URLS are not matched, I redirect the user to another domain?
For example the part after /v/ is always a 32 character MD5 string. the :id part is always a number.
If you could give me a regex (regex has alas never been my forte) example for
/v/3b89441db7986135e5eb9e1debf0cc23
and
/v/3b89441db7986135e5eb9e1debf0cc23/edit-attendee/:id
that would be excellent.
This regex matches your urls:
/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?
In english...
/v/ is a literal
[a-f0-9]{32} means 32 hex digits
/[a-z-]+(/\d+)? means "/" then at least 1 of (any lowercase letter or a dash) then "/" then some digits
surrounding a regex in (...)? means either one or none of them
FYI, this regex matches all urls given in question reasonably tightly
If you want to not match, use this:
^(?!/v/[a-f0-9]{32}(/[a-z-]+(/\d+)?)?$)
How about this:
\/v\/[a-f0-9]{32}(\/(login|logout|access|delete-attendee\/:\d+|edit-attendee\/:\d+|finalise)?
This will only match your accepted urls. You should adjust for your flavor or regex and appropriate escape chars.
You said that you want to redirect if URLs do not match, so first you must make a rule that matches all valid URLs, then prepend ! to negate the match.
RewriteEngine On
RewriteRule !^v/[0-9a-f]{32}(/(login|logout|access|delete-attendee/\d+|edit-attendee/\d+|finalise))?$ http://your-other-domain/ [R]
The above rule should be placed in the .htaccess file present in the root directory of your website.
Related
I want to check if a parameter is present in a url in nginx and then rewrite. How can i do that?
The color is dynamic in the URLs
For e.g
If url is http://website.com/lunch-box/xxxxxabc then redirect user to http://website.com/lunch-box/.
If URL is http://website.com/lunch-box/xxxxxabc/ABCD123 no need to redirect. Need to load as it is.
I want to redirect if URL is matched. and xxxxxabc is dynamic text.
nginx version: nginx/1.16.1
# rewrite direct children of /lunch-box but not grandchildren+
rewrite ^(/lunch-box/)[^/]+/?$ $1 last;
Walking through the regex ^(/lunch-box/)[^/]+/?$
^ matches the start of a string (rewrite rules match against the path, not the full URI)
(/lunch-box/) matches the literal text /lunch-box/ and saves it for $1
[^/]+ matches one or more characters that are not a forward slash
/? matches zero or one forward slash
$ matches the end of the string
This strips off the path past what we've saved as $1, but only when that path is a direct child.
I have an ecommerce store with the product URL format:
/categoryname/subcat1name/subcat2name/1450--my-widget
I will shorten it to:
/1450--my-widget
I can do the change within the ecommerce software, but I need to set up a mod rewrite redirect for the old URLs.
To avoid matching URLs for categories, content pages, etc, as well as product URLs of the new format, I need to match on all these conditions:
Does not contain the string "/info/"
Contains a slash, followed by 1 or more characters, followed by another slash, followed by 1 or more digits, followed by "--", followed by 1 or more characters
What directive would work?
EDIT:
More examples of matching and non matching strings
Matches for old product url:
/a-category/this-category/333--my-widget
/some-cat/34--widgetname
Non matches:
/1918--widgetcategory/
/info/12--about-us
/quick-order
/login?back=my-account
/2050--my-widgetname
You can use this 301 redirect rule as your very first rule in site root .htaccess:
RedirectMatch 301 ^(?!.*/info/).*/[^/]+/([0-9]+--[^/]+)/?$ /$1
I'm trying to make it so that if the ending of a URL contains anything surrounding a number (except that the first part can be any combination of numbers, a hyphen or a p), then the url is redirected with whatever surrounding the number is taken off.
Here's my regex:
RewriteRule ^all/[^p^P^0-9^\-]+([0-9]+).*$ /allof/$1 [R=301,NC,L]
If I tried these test URLs, the redirect should happen, but does not:
http://example.com/all/-a*1
http://example.com/all/plus100
If I tried this test URL, the redirect does not happen which is correct:
http://example.com/all/p1-100
If I tried these test URLs, the redirect happens, which is correct:
http://example.com/all/(100) - redirects to http://example.com/allof/100
http://example.com/all/minus100 - redirects to http://example.com/allof/100
Perhaps my regex is faulty. I tried removing the extra carets in the square brackets except for the first, and that didn't help, and I don't want to replace the square brackets with only a .* since I then won't be able to capture the number. What could I be doing wrong?
You can use negative lookahead in your rule:
RewriteRule ^all/(?!p\d*-)\D*(\d+) /allof/$1 [R=301,NC,L]
RegEx Demo
i want to page redirection and unable to write condition for it
have different scenario want to redirect friendly url to query string base
http://www.domainname.com/directoryname/friendly-url-goes-here_123456.html
friendly-url-goes-here can be like this friendly_url-goes_23-here_123456.html, i just want 123456
and page get redirected to this
http://www.domainname.com/detail-page?id=123456
123456 will be a variable
You can use this lookahead based regex:
\d+(?=\D*$)
RegEx Demo
.htaccess:
Inside your root .htaccess you can use this rewrite rule:
RewriteEngine On
RewriteRule ^directoryname/.*?(\d+)\D*$ /detail-page?id=$1 [L,QSA,NC,R]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
Apache mod_rewrite In-Depth Details
May be this could be one way to do it:
(\d+).html$
\d+ match a digit [0-9]
Online Demo 1
and this is another way:
\d+(?=\.html$)
(?=.html$) It is a positive lookahead - it assert that the regex below can be matched if it contains at end($) a .html
Online Demo 2
I have an interesting RewriteRule and I am not sure to make the regular expression.
In short I would like to examine the URL after the second / - based on the contents, possibly forward.
It sums down to these two rules:
Redirect if string after second / contains any letter
Redirect if string after second / is longer than 7 characters
Here are examples:
http://www.mysite.org/section/subsection/2011 (OK, do nothing)
http://www.mysite.org/section/subsection/2011-11 (OK, do nothing)
http://www.mysite.org/section/subsection/2011-11-09 (NOT OK, redirect)
http://www.mysite.org/section/subsection/2011-11-W1 (NOT OK, redirect)
Please help me with the Rewrite Rule and Rewrite Cond
I based the following regex off of your examples which seemed to analyze everything after the last '/' instead of the second.
/[^A-Za-z]{1,7}$
In plain English, this regex matches a group of characters that follows a '/' and contains 1-7 non-alphabetical characters. To make it only match the last '/' I appended a '$', which matches the end of a line.
Also, since you are dealing with URLs you might want to add "/?" before the '$' in case there is a dangling '/' at the end.
/[^A-Za-z]{1,7}/?$
I hope this answers your question.
As Lane Aasen points out, your examples and rules don't match up. I'm taking your examples to mean that the rules should apply to the 3rd path segment, and suggest the following. You don't say whether the redirect destination is different or not depending which rule matches. I've taken it to be different.
RewriteEngine on
# redirect if any letter is contained in 3rd part of path
RewriteRule ^([^/]+/){2}.*[a-zA-Z] http://www.bbc.co.uk [R=permanent,L]
# redirect if there are more than 7 characters in 3rd part of the path
RewriteRule ^([^/]+/){2}.{7,} http://www.ibm.com [R=permanent,L]