I am out of my depth here, currently reading the tutorials and using python to learn regex.
I have a website where a php file http://www.example.com/showme.php?user=JOHN will load the visitor page of JOHN. However I want to let John have his own vanity URL like john.example.com and rewrite it to http://www.example.com/showme.php?user=JOHN .
I know it can be done and after fiddling with it it seems lighttpd mod_rewrite is the way to go. Now I am stumped as I am trying to come up with regex to match!
rewrite ("^![www]\.example\.com" => "www\.example\.com\?user=###");
I am playing with python re module to test out several ways of getting the john from john.example.com and recognize when the first segment of url is not www and then redirect. Above was my trial. Am I even in the right continent!
Any help will be appreciated in
recognizing when first part of url before the first . is not www and is something else - so that example.com won't stump it.
getting the first part of the url before first . and tag it to user=###
Thanks a bunch
Use lighttpd's mod-rewrite module. Add this to your lighttpd.conf file:
$HTTP["host"] != "www.example.com" {
$HTTP["host"] =~ "^([^.]+)\.example\.com$" {
url.rewrite-once = (
"^/?$" => "/showme.php?user=%1"
)
}
}
For an href value like /dir/page.php the domain part of the link gets automatically added from the current request as shown in the browser's address bar. So, if you had used www.example.com; the link would point to htp://www.example.com/dir/page.php and likewise for john.example.com.
For all your links to point at www.example.com, you need to be accessing the page using www. This would be possible only if you do an external redirect from the vanity URL to the actual one i.e. users can still use the shortened URL but they would get redirected to the actual one.
$HTTP["host"] != "www.example.com" {
$HTTP["host"] =~ "^([^.]+)\.example\.com$" {
url.redirect = (
"^/?$" => "http://www.example.com/showme.php?user=%1"
)
}
}
Related
I had the following pattern :
https://www.example.com/category-name/2438-name.html
https://www.example.com/random-category-name/42342-name.html
So the structure is :
/[category-name]/[product-id]-[product-name].html
Today, for SEO reasons I changed the url structure to the following:
/[product-id]-[product-name].html
So I excluded the /[category-name] from any product page
I am looking for a htaccess rule to redirect any old request to a new url.
Example:
https://www.example.com/cars/4322-tesla-model3.html (now returns 404)
The new (correct) url is : https://www.example.com/4322-tesla-model3.html
I don't know how to build this regex, may anyone can help?
I am looking for proper handling of URLs in Varnish (5.2.1), here is what I do (trying to redirect to lowercase URLs):
set req.url = std.tolower(req.url); //this is new.url
//if original.url != new.url => redirect
This produce good URL, until client library (and there are quite few) where they convert %[hex] to %[HEX] according https://www.rfc-editor.org/rfc/rfc3986#section-2.1 end up in URL redirection loop.
Example:
req.url = "/query=mythbusters%20-%20die%20wissensj%c3%A4ger"
is redirected to
"/query=mythbusters%20-%20die%20wissensj%c3%a4ger"
and client redirects it to
"/query=mythbusters%20-%20die%20wissensj%c3%A4ger"
I am trying to solve this issue, using regular expressions, but for some reason, I can not get UPPER case results, according PCRE/PCRE2/Perl regexp it should be possible like this:
set req.url = std.tolower(req.url);
set req.url = regsuball(req.url, "(%[0-9a-f][0-9a-f])", "\U\1");
Anybody have idea how to solve this ?
I posted issue on Varnish github, answer was this is not supported.
I have written nginx rewrite rule to redirect all request for /path/category except subcategory1. I am using below regular expression for match and it is working fine in regex tester. However, when I am providing same regex in Nginx conf then it is not working for negative lookahead if url contains the # character. Do you have any suggestions?
Regex tried so far:
^\/path\/category(?!.*(\bsubcategory1\b)).*$
^\/path\/category(([\/#]*)(?!.*(subcategory1))).*$
Rewrite Rule:
rewrite ^\/path\/category(?!.*(\bsubcategory1\b)).*$ https://new.host.com permanent;
Path Details:
It should redirect to https://new.host.com which is working fine
/path/category
/path/category/
/path/category#/
/path/category/#/
skip the redirection for subcategory1 . It is not working for last 3 urls that contains hash.
/path/category/subcategory1
/path/category/subcategory1/
/path/category/subcategory1/dsadasd
/path/category#/subcategory1
/path/category/#/subcategory1
/path/category#/subcategory1/dadsd
Anything in the URI after # is ignored because it is supposed to be client side so it never gets to HTTP server (Nginx for instance).
Nginx regex will show abnormal behavior if a # is in the string under processing.
The part after # is called fragment.
The fragment can be processed at client side.
You can use window.location.hash to access and process fragments.
This Javascript example transform fragment in parameters in a request to process.html :
let param = window.location.hash;
param = param.substring(1); // remove #
param = '?' + param;
console.log('param=',param);
location.href = '/process.html' + param;
I have wordpress pagination and I want that the www.site.com/page/1235x returning me a 404.
At the same time I don't want to disable the pagination for the entire site because I use it with other url, like /categories/page/2 or /page/2/?s=search-item .
So, I thought I need to write a nginx regex that match only a piece of the url.
I have three example url:
1. www.site.com/page/2/
2. www.site.com/page/2
3. www.site.com/page/2/?s=term-of-search
For the first and second I want to return 404 with something like this:
location ~* ^/page/\d+/?$ { return 404; }
And this matched.
For the third I don't want nothing, but that normally goes the search result.
Unfortunately, my test always select the part www.site.com/page/2/ returning 404.
I've done a lot of test with ngnix regex, also with order of them,
but I can't find a solution to this.
Any idea?
Thanks
I have the following url´s types in my application:
http://192.168.0.191/myapp/public/controller_name/action_name
http://192.168.0.191/myapp/public/controller_name/action_name/param1/param2
http://192.168.0.191/myapp/public/controller_name/action_name?parameterlist
Ans I´m using the following REGEX for redirection:
I have the following Regex for redirection (everything goes through the index page):
"^([a-zA-Z0-9\/\-_]+)\.?([a-zA-Z]+)?$" redirects to "/myapp/public/index.php?url=$1&extension=$2")
Here are examples of URLs used:
http://192.168.0.191/myapp/public/customers -> Call customer controller
http://192.168.0.191/myapp/public/customers/detail/2 -> Show customer detail for id = 2
Both works fine. My problem is with the URL below - to load customer data for a Datagrid object:
http://192.168.0.191/myapp/public/customer/loadDtData?draw=1&columns%5B0%5D%5Bdata%5D=0&columns%5B0%5D%5Bname%5D=&columns%5B0%5D%5Bsearchable%5D=true&columns%5B0%5D%5Borderable%5D=true&columns%5B0%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B0%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B1%5D%5Bdata%5D=1&columns%5B1%5D%5Bname%5D=&columns%5B1%5D%5Bsearchable%5D=true&columns%5B1%5D%5Borderable%5D=true&columns%5B1%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B1%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B2%5D%5Bdata%5D=2&columns%5B2%5D%5Bname%5D=&columns%5B2%5D%5Bsearchable%5D=true&columns%5B2%5D%5Borderable%5D=true&columns%5B2%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B2%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B3%5D%5Bdata%5D=3&columns%5B3%5D%5Bname%5D=&columns%5B3%5D%5Bsearchable%5D=true&columns%5B3%5D%5Borderable%5D=true&columns%5B3%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B3%5D%5Bsearch%5D%5Bregex%5D=false&columns%5B4%5D%5Bdata%5D=4&columns%5B4%5D%5Bname%5D=&columns%5B4%5D%5Bsearchable%5D=true&columns%5B4%5D%5Borderable%5D=true&columns%5B4%5D%5Bsearch%5D%5Bvalue%5D=&columns%5B4%5D%5Bsearch%5D%5Bregex%5D=false&order%5B0%5D%5Bcolumn%5D=0&order%5B0%5D%5Bdir%5D=desc&start=0&length=25&search%5Bvalue%5D=&search%5Bregex%5D=false&_=1450111563514
How can I fix the Regex to separate the text after the last / but before the ? and then text after the ? to pass as parameter to the application.
Thanks for helping. I´m using lighttpd as web server.
OBS: For Apache I would use the RewriteRule below to make the trick, but lighttpd does not have support for QSA:
RewriteRule ^([a-zA-Z0-9\/\-_]+)\.?([a-zA-Z]+)?$ index.php?url=$1&extension=$2 [QSA,L]
In absence of QSA flag try this rule:
url.rewrite = ( "^/([\w/-]+)\.?([a-zA-Z]+)?(?:\?(.*))?" => "/index.php?url=$1&extension=$2&$3" )
Reference