I want to redirect the URL to absolute URL on top of existing rule.
Suppose I have a existing rule as below
rewrite ^/xyz/(.*) /new-url redirect; this is working fine
I want a new url with a query param on top of it and that supports specific condition . I would want above URL also to work
/xyz/?infoid=color to be redirect to www.abcde.com
I tried this by adding the rule above the existing one as per order rule but didn't work.
rewrite ^/xyz/?infoid=color$ www.abcde.com redirect;
Is there any solution for this in nginx?
Related
I need to redirect some urls to their original base url, for eg if if i have the below url
https://example.com/path/to/something
it should be redirected to the below
https://example.com
Any way i can acheive this on nginx level or cloudfront level?
I tried the below
rewrite ^(/path/to/something)(.*)$ https://example.com/$2 permanent;
but did not work
If you want to do this in CloudFront, you can use CloudFront Functions (lightweight JavaScript functions that execute at the edge). Here is an example of doing redirects based on country - you can modify based on your needs:
https://github.com/aws-samples/amazon-cloudfront-functions/tree/main/redirect-based-on-country
I have mainsite.com and a subdomain sub.mainsite.com.
The sub.mainsite.com has a cname record point to another online content web
Right now I will to redirect mainsite.com/sub to display the sub.mainsite.com content, but still keep the mainsite.com/sub URL.
How can I do that with Nginx?
You cannot do that. You can either redirect using return or rewrite directive. Either way, the URL is redirected. The difference is you can reframe the URL as required using rewrite directive.
For a site running Mediawiki, I have this config for clean URLs:
rewrite ^/wiki/([^?]*)(?:\?(.*))? /wiki/index.php?title=$1&$2 last;
This works fine except when the page title needs to have a question mark. For a URL like /wiki/Who_is_your_daddy%3F_It_is_me the correct rewritten URL is /wiki/index.php?title=Who_is_your_daddy%3F_It_is_me. However, Nginx is rewriting to /wiki/index.php?title=Who_is_your_daddy&_It_is_me.
What is the correct rewrite rule?
The query string is not part of the normalised URI that is processed by the rewrite directive's regular expression. However, the query string will be automatically appended to the rewritten URI, so you don't need to do anything to include the &action=edit part. See this document for details.
The %3F in the original request will be normalised to a ? by the time that rewrite is processing it. You will need to capture both sides of the ? to manually translate it back to %3F in the new query string.
You can use more than one rewrite statement, so that the case with and without a %3F are both handled correctly.
For example:
rewrite ^/wiki/(.*)\?(.*)$ /wiki/index.php?title=$1%3f$2 last;
rewrite ^/wiki/(.*)$ /wiki/index.php?title=$1 last;
I'm trying to redirect
searchresults.php?categoryarray[]=Accessories
to
search-works.php?category=Accessories
rewrite ^/searchresults\.php(.*)$ /search-works.php?category=$arg_categoryarray[]? redirect;
The array in the old site url isn't really used so can assume a single 'category'.
My redirect is resulting in just "[]" as the category, and if I remove the brackets from the rewrite, then I get nothing from $arg_categoryarray.
Tarun's comments solved this.
I went with creating a matching php file to do the redirect.
I want to replace my url via Nginx's rewrite directive. For instance, the client side requests http://127.0.0.1/user/user_id/, and I want to let Nginx rewrite the url to http://127.0.0.1/person/person_id/.
My Nginx configuration is like this:
rewrite (.*)user(.*) $1person$2;
But I fount the Nginx changes the url to .../user/person_id/
Could someone tell me how to change the user to person via rewrite directive?
Assuming that the first instances of user and personare constant and that there is always a slash after the second item, you can try:
rewrite ^/user/user_([^/]+)/(.*)$ /person/person_$1/$2 ;
well this single case you are talking about can be solved simple:
rewrite ^/user/user_id/(.*)$ /person/person_id/$1 ;