regex pattern for url validation to make http as required - regex

I'm stuck at regex pattern.
Currently I'm using regex:
https?:\/\/(?:www\.|(?!www))[^\s\.]+\.[^\s]{2,}|www\.[^\s]+\.[^\s]{2,}
but it takes www.google.co.in as a valid url but I want http:// or https:// as a required part of url.
Is there any way I can achieve that?

Related

Django greedy characters missing in reverse urls

I am trying to use the following rule to map urls in django
url(r'^(?P<permalink>[a-zA-Z0-9_-]*)/?$', views.page, name='page'),
This should match pages like
site.com
site.com/super-awesome-page/
This works however the reverse urls provided by the url template tag are missing the trailing / i.e. "site.com/page" these do get captured pattern but I want my links to show up in my page correctly how can I get this to work correctly.
I would have expected since the trailing slash is greedy it should be included in the reverse url.
The trailing slash is optional in your regex, so django doesn't generate it for you for the reverse URL.
The easiest solution is probably to make the trailing slash non-optional. With the default settings, django will redirect the non-slash version to the slash-version. Easiest to just standardize on that.

Regular expression to validate url with www but optional http or https

I want to have a regex that will validate url in following way
http://www.google.com ->valid url
www.google.com ->valid url
google.com ->in-valid url
http://google.com ->in-valid url
I have tried following regex from here
/(?:https?:\/\/)?(?:[a-zA-Z0-9.-]+?\.(?:[a-zA-Z])|\d+\.\d+\.\d+\.\d+)/
but it doens't validate the existence of www
You can use this regex in PHP:
$re = '~\b(?:https?://)?(www\.[^.\s]+\.[^/\s]+|\d+\.\d+\.\d+\.\d+)~';
RegEx Demo
This regex will enforce www. after optional htpp:// or https:// before it.
Another option in PHP is to use parse_url function and check for result['host'] array entry.
Put www in your regex:
/^(?:https?:\/\/)?(www(\.[\w-]+)+|\d+(\.\d+){3})/
I also improved it a bit (so it doesn't match "www......" etc)
See live demo.
To match www you will have to use regex like this.
Regex: (?:https?:\/\/)?www\.[a-zA-Z0-9.-]+.[a-zA-Z]+|(?:\d\.?){4}
Regex101 Demo

Get Complete String after specific word using regex

I want to redirect 1000 of URL'S using wordpress Redirection plugin with regex.
Here is the my URL
http://domain.com/blog/tag/washington/
need to redirect on
http://domain.com/tag/washington/

IIS7 URL Rewrite with Regex

I'm trying to do a URL rewrite when a user accesses a certain URL of my site:
Accessed URL: https://client1.domain.com
Rewritten URL: https://new-client1.otherdomain.com
My site has many URLs that point to it, so the simple HTTP redirect module will not be a valid solution for this. What would the Regex be and what would I want to fill in for each section in a rewrite rule?
Thanks
Try this:
s/client1.domain/new-client1.otherdomain/g
You can use this regex pattern to search for client1.domain in order to replace it:
(?<=//)([^.]+)\.domain
Replace it with a backreference to client1 and the new domain like so:
$1\.otherdomain

how to convert sub-domain URL to domain URL using smarty

I have a URL like this
http://subdomain.domain.com/xyz-200_some_information
I want to convert this URL to following URL using smarty.
http://www.domain.com/xyz-200_some_information
That is, need to replace subdomain with www
subdomain is not fixed, there may be more more subdomains. I'm looking for a smarty based solution instead of a PHP one, as I don't have to acc change
You can use a regex_replace
{$var|regex_replace:"/http:\/\/.*?\./":"http://www."}
This matches a region from http:// to the first dot and replaces it with http://www.