Yii2: Rewrite URL without the help of a webserver - yii2

I would like to rewrite an URL purely using Yii2, without relying on a webserver (nginx/apache).
Example. I would like to "rewrite" a url such as [POST|PUT|GET] /v2/book/author into [POST|PUT|GET] /v2/document/author.
My first idea was to use UrlManager's $rules, but those are not really rewriting the URLs, they map URLs into routes, so as soon as the first one matches, the chain terminates.
URL rewriting needs to happen before UrlManager even begins processing the routes.
My second idea was to create a custom UrlNormalizer which simply substitutes the incoming URLs. This sounds better, but I don't know if it will work.
What is the right way to do this?

Urls are automatically created by Yii if you don't specify it.
By default <module>/<controller>/<action>/<param>
If you have a problem, where you would have to refactor the whole code for an url change, then yes, UrlManager it is. Url::to() uses your url config to adjust your <controller>/<action> identifier into an actual url. If you hardcoded the urls then you have to change the urls manually.
But you should be able to create to create a matching pattern to catch v2/book/author
I tried this in my code, and it worked:
So this should work as well

Related

Hide url in anchor tag

abcv
If i click this it's showing complete address. Problem is the path is shared and anyone can hack this path. Is there any way to not to show this entire path and only show abc.jpg in browser.
You may use following approaches
Store the images in the database and use HttpHandler to retrieve and display them.
As additional precaution you may pass current DateTime as encrypted url parameter to verify whether it is a fresh request within a specific time period say 10 minutes. You may refer this article for reference to HttpModule implementation
display-images-from-database-in-gridview-in-aspnet-using-handler-ashx-file-in-csharp-vbnet
As another approach you may implement the concept of Temporary URLs as described here Generating Temporary Download URLs
No. The complete path has to be shown so that browsers can retrieve the file.
You could implement a custom HTTP Handler or asp.net page that takes the name of the file in the query string and returns the contents of the file, perhaps even using a unique id (number, guid, etc,...) to map to each file to stop people "guessing" what other valid filenames may be. So, you'd have:
http://ipaddress/RetrieveUploadFile.aspx?fileid=36281
instead of
http://ipaddress/uplodfiles/2/abc.jpg
No, you don't want to "hide URL"; the whole notion would not make any sense, because all HTTP requests go with some URL. What you want is called URL rewriting:
Please refer this and this links

Create a unique URL like facebook

How exactly does one do something like create a unique URL.
Like how facebook does it facebook.com/mynamehere
One way would be to create multiple folders each time we have a new user..but that doesn't seem to be the best approach
You can try a program like Elgg if you are trying to build a social media site. Otherwise, a person's profile can be custom in a couple of ways. Most of them mentioned. You, as mentioned, can use .htaccess for rewrites. You can use an automated custom url plugin (this may help: How to generate a custom URL from a html input?). Similarly, you can use the previously mentioned Elgg for social media, and but also as a last resort can use your folder method, but only if absolutely required.
I think the question is: how is it done technically, so we don't need to have physical file for every valid URL?
The answer is URL rewriting. In case of Apache server, you want to enable mod_rewrite and configure it to translate particular URL pattern (like myfbclone.com/mynamehere to myfbclone.com/index.php?username=mynamehere). This way you need to have one script file that handles all the URLs accordingly.
Different servers have different means of rewriting URLs, like Nginx or IIS, so the exact way of configuration depends on your server, but the concept is usually the same.

is homepage url and homepage url + / ever different?

Let's say we have a site that's test.com, would test.com/ ever be a different site/file then test.com? I know that the url represents a path to the server to get that file. Going to test.com/file and test.com/file/ could potentially bring up different sites since the latter is a directory. So I was wondering if the same is true for the root.
Or am wrong about the url as well?
It all depends on your environment and how it handles the routing. Many frameworks treat url/ and url as aliases, but many frameworks don't. So the answer to your question is yes, it can be different.
the url represents a path to the server to get that file
This is correct, but this can be any path you choose.
When you create a simple website with nested folders yes you can create something like this:
/webroot/index.html
/blog.html
/myvideos/list.html
Which results in for example www.example.com, www.example.com/blog.html and www.example.com/myvideos/list.html
But with some server side settings called rewrites you can make your url behave like anything you want.
I could even redirect urls to entire different servers. Or make 2 different urls go to the same path. Anything you want.

how to deal with www/http in href

I have a db with a buch of urls. The values were entered by users, so it might be something like www.domain.com or http://www.domain.com or stackoverflow.com or https://something.com
I'm retrieving that data and creating links in a html page so people can click and be redirected to that url.
If i get the url from the page , i'll have either:
1.<a href="www.domain.com">
or
2.<a href="http://www.domain.com">
in the second case it works, but the first it doesn't.
Is there a way to make it always work?
thanks!
The www. bit is not special at all, people rely on an automatic correction feature of most browsers to prepend it if the host does not exist. To replicate this, you need to run a program that attempts to resolve each of the host names in your database, and retries with an extra www. if that fails.
The http:// bit is easy: if it is missing, add it.
There are two ways to handle this situation:
First, validate the user input. At the time a URL is submitted, validate it (preferably on the client side via Javascript) to ensure it has the required elements.
Second, in your code, you can use a regular expression or even simple pattern matching to ensure that the string starts with 'http://' or 'https://', and prepend it as needed.
The implementation details vary from language to language, but the concept is the same.

How to keep a url param across pages

I wan't developers who embed my webapp to be able to pass a param in the url like ?style=dark which will alter the css accordingly. Is there a better way to keep this setting as the user navigates than appending ?style=dark to all links?
I've considered cookies etc. but if one user is viewing two pages which embed my app with different themes then one will override the other.
I'm using Python/Django.
If you neither want to use Cookies nor Sessions and do not want to embed it into URLs, the only alternatives which comes to my mind are:
First the most generic: Use a dummy domain in front. Instead of www.example.com use h**p://THEME.example.com/PATH. This even works for HTTPS if you own a wildcard SSL for *.example.com.
A second variant would be to create a Basic-Auth-Handler which uses the Theme as the username with a dummy password. The URL then can look like:
h**p://THEME#www.example.com/PATH
However I am NOT sure what happens if a user connects to the same site with two different themes in the Basic-Auth-case. Also it is somewhat tricky to make the site available for search engines if it is behind an authentication handler. This is because you MUST have an auth-handler today to circumvent the Anti-Phishing-protection in modern browsers.
Note that with both methods you can only have one parameter easily. The password does not count and there are browsers out there which do not accept a wildcard SSL cert *.example.com for PARAM1.PARAM2.example.com.
I'm not exactly sure, but anyway you would have to pass this variable to your site. With that I mean, that there is no difference if you add ?style=dark to your href's, or rel="dark" to your <a>'s for use with javascript. keep in mind that it's just an example
Ofcourse you can always work on that AI to predict what the user wanted at the specific moment. hehe
I'm sure you can use a Session for this kind of thing, not? The first time the values are provided via the Querystring you add them to the Session and then retrieve them from the session in the future.