Sorry, I don't really know how to word the title but this has been bothering me for ages. I have always wondered how on websites sometimes you see a weird url structure. Take youtube for example:
youtube.com/c/this_persons_channel
I just don't understand how it works! I understand that the / means it is a folder within the website but how it ends off. Is it a file? and if so, where is the file type? And is this file made from code? because it is for a specific user. and It can't of been there when they made the site.
I'm sorry if this is a stupid question but I have no idea what to search for to find out more about this.
CodePudding user response:
I think what you're talking about is rewriting the url. This can be done in the .htaccess
file of your project.
The end of the url is not a file or a folder. It is a query string. which can be accessed by code and used for custom pages. take the url for this question:
https://stackoverflow.com/questions/69684211/what-is-the-unusual-thing-that-i-sometimes-see-in-website-urls
That can also be represented as:
https://stackoverflow.com/questions?id=69684211&name=what-is-the-unusual-thing-that-i-sometimes-see-in-website-urls
Note: These are probably not the actual names for the query strings, just an example.
By writing:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^index/([0-9] ) index.html?id=$1 [NC,L]
In the .htaccess
file, you can turn the url
http://localhost/index.php?id=5
Into
http://localhost/index/5
Note: This will not simply change the url when you refresh the page. But if I were to type in 'http://localhost/index/5' it will give me the same page as if I typed in 'http://localhost/index.php?id=5'. So you will have to change all redirects to that page to the prefered url. All this code does is make it so it doesn't send you to a 404 error page. If you want, you can probably write some php code that automatically redirects you if you are on the original url.
Websites do this for a couple of reasons:
- Makes the url more memorable and easier to write,
- Makes it easier for search engines to find the website.
Explaining the .htaccess code
Once you understand what the code means it is actually fairly simple.
Line 1:
RewriteEngine on
By setting this to on, it allows you to rewrite and make your own rules for the url.
What is a rewrite engine?
Line 2:
RewriteCond %{REQUEST_FILENAME} !-d
This line is a condition, kind of like an if statement, where it is saying if the Requested File name is not a directory, then continue with code. This is to prevent the issue where a file has the same name as a folder and it can get confusing in the url.
Line 3:
RewriteCond %{REQUEST_FILENAME}\.php -f
This line checks if the requested file has the extension 'php' it doesn't have to be php, it can be whatever you want.
For more information on .htaccess rules and conditions: .htaccess rewrite rules
Line 4:
RewriteRule ^index/([0-9a-zA-Z_-] ) index.php?id=$1 [NC,L]
Ok so this is the most confusing line. It is basically describing the rule for what to change the url to.
The '^' right at the beginning means ignore anything in the url before what comes after it. So it would ignore whatever comes before 'index' in the url.
Now you want to set the rules for what characters are allowed between index/
and the next '/' in the url. If you only have one query in the url then don't worry about adding another '/' at the end. But if you have lets say a ?name
parameter, the url will be https://localhost/index/5/charlie
for example. but because I have two search parameters, I have to add another regex. So the last line will now become RewriteRule ^index/([0-9a-zA-Z_-] )/([0-9a-zA-Z_-] ) index.php?id=$1 [NC,L]
In this example it allows all characters from 0-9, a-z (lowercase and capitals), and underscores and hyphens. So it will rewrite the url only if it contains those characters. If it contained a dollar sign $
, for example, then it wouldn't rewrite.
Make sure you include the after the closing square bracket! Without the at the end, it would only allow 1 character values. So ?id=5
would work but ?id=52
wouldn't work.But with the there it will be fine.
Now, the next part of the line is telling the server what you want to redirect to in the url. In this case you want to redirect to index.php
so you write that.
You also want to include the parameters so you can actually use them in your code. So now add on the query string/s. But we don't know what the ?id
parameter is. So we need to put a placeholder there. So now type in ?id=$1
. Make sure the variable is called 1 and not something else.
If you were to have more than one parameter, you would simply write ¶m2=$2
then ¶m3=$3
and so on.
The final thing you need to add is [NC,L]
Where the NC
means Non Case
which means if someone were to write index.php with a capital I, it would not matter. And where the L
means the Rewrite Conditions from earlier only apply to that rule and not any rules you may have later on.
If you didn't understand my explanation watch Dani Krossing's video on it, he explains it very well: https://www.youtube.com/watch?v=zJxCq6D14eM