URL Modification with GET Parameters

Hello!

I have a website that relies on GET parameters to show users the correct information. However, it looks kind of ugly this way, so I wanted to change it so it looks like a normal file structure.

Example:
Before

https://domin.tld/folder/viewAccount.php?account_id=id_10464141

After

https://domin.tld/folder/viewAccount.php/account_id/id_10464141

I was hoping to accomplish this with PHP. I am sure there are plenty on online guides on the subject (And it has certainly been done before) but I do not know what this is called, so my searches yield no results.

The only other thing is that PHP still needs to read it as a GET parameter in order to collect the correct information from the database and display it.

Thanks for any assistance!

It is known as “Pretty URL’s”.

1 Like

It is typically done by the server, Eg: on Apache in the .htaccess file. Though there are ways in PHP.
Looking at your examples, if you are going to do this, go all the way and have something like:-

https://domin.tld/viewAccount/10464141

Less is more.

2 Likes

You can create mod rewrite rules in a .htaccess file for a server using apache. However, the more modern approach is to ROUTE the request in the application itself not using archaic server rewrites. This is one of many great things most frameworks will handle for you.

Yes, if you are using a framework, it may well handle it all for you.
But if you are not, and don’t want to use .htaccess (you may not even be on Apache) you can pull parts of the URL into your router script from values in the $_SERVER global and deal with it there

Yes, I am on Apache, and yes, I can use htaccess. After I got the name from @benanamen, I took a look on the web and found this code:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^account_id/(.*)$ ./viewAccount.php?account_id=$1

I will be testing it out later, but it should work, correct?

1 Like

Optimally you want a single point of entry for the site. Learn about Front Controller and MVC.

1 Like

Yes it would.

Also, what @benanamen said. But in the meantime, this will work just fine.

1 Like

That looks pretty complicated. With over 250 pages on the domain, with plans to add an additional 200, I think that would only slow things down, and use more resources.

Nah not really. It’s just a different approach to do the same thing.

I actually think having a single entry point will simplify your work with many pages. Once you get your head around the idea.
If you are already doing things like:-

viewAccount.php?account_id=id_10464141

You are familiar with the idea that the one script on page viewAccount.php can display any number of different web pages.
It is really just an extension of that concept. So if all you had in the public root was index.php, the script in that file (or possibly one required by it) could show absolutely any number of pages, depending on what was in the requested URL.

3 Likes

Application level routing is much more flexible. For example, users can define component paths saved to a database. When users access the path the mapping can dynamically be discovered by querying the database. Mappings can be dynamically loaded in such a way in .htaccess without resorting to passing the path into the app layer and having it discover the component to render.

For example, in the platform I’m creating users can create path mappings for two separate assets.

  • pages
  • external applications

The database pictured below can be queried for a match and app can then render the associated component in the json for the mapping. The discovery strategy is currently based on matching the site and path of the object. The site filter isn’t included below because currently there is only one site.


I also have future plans to extend this discovery mechanism to not only include site, path but other attributes as well like auth info, device, screen size. Where a user accessing a device on one device might see a different page than a user accessing on another device type like a phone. Another example could be dedicated displays for specific users based on auth info rule matching.

This is the mechanism by which this demo page is pre-rendered to static html.

https://ng-druid.github.io/dev-test-virtual-list-flex-v1/character/1011334

This doesn’t use a server. Instead the query to discover the route is done directly in the browser. That can be seen by looking at the request pictured below. The domain is *.amazonaws.com is aws not a proxy or middle layer but straight up aws managed open search instance.

This doesn’t use a server. Instead the query to discover the route is done directly in the browser. That can be seen by looking at the request pictured below. The domain is *.amazonaws.com is aws not a proxy or middle layer but straight up aws managed open search instance.

This is JavaScript or more specifically Angular. None the less, the same fundamental dynamic routing can be achieved with all languages and many frameworks. The only difference is that instead of doing it in the browser the routing would be handled on the server in the app layer.

Ok, I must be missing something here.

URL: http://domain.tld/FOLDER-NAME/viewaccount?account_id=accnt_31219451

As evident from the code below, I am still learning .htaccess, and just tried a bunch of random stuff that I hope would work.

.htaccess code:

IndexIgnore * 
Options -Indexes

Options +MultiViews +FollowSymLinks

RewriteEngine On

RewriteBase /

#Remove .html and .php extension (THIS WORKS)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]


#Attempt no.1
RewriteRule ^([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.2
RewriteRule ^([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.3
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.4
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^/FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.5
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount.php?account_id=$1
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount.php?account_id=$1
#Attempt no.6
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)$ viewaccount?account_id=$1
RewriteRule ^FOLDER-NAME/([a-zA-Z0-9_-]+)/$ viewaccount?account_id=$1
#Attempt no.7
RewriteRule ^account_id/(.*)$ ./viewaccount.php?account_id=$1
#Attempt no.8
RewriteRule ^account_id/(.*)$ ./FOLDER-NAME/viewaccount.php?account_id=$1
#Attempt no.9
RewriteRule ^account_id/(.*)$ viewaccount.php?account_id=$1
#Attempt no.10
RewriteRule ^FOLDER-NAME/account_id/(.*)$ ./viewaccount.php?account_id=$1
#Attempt no.11
RewriteRule ^FOLDER-NAME/account_id/(.*)$ ./viewaccount?account_id=$1
#Attempt no.12
RewriteRule ^account_id/(.*)$ ./viewaccount?account_id=$1
#Attempt no.13
RewriteRule ^account_id/(.*)$ ./FOLDER-NAME/viewaccount?account_id=$1

.htaccess location: Root Folder (I also tried it in the “FOLDER-NAME” folder)

And none of these works? I would expect at least attempt 9 to work…

Does it show an error instead?

.htaccess can show error messages? How would I turn those on?

I think my hosting provider may be restricting the function, but I will check it again later since I may have mis-uploaded it due to getting angry about it.

.htaccess can’t
The website might

Maybe check the Apache2 error logs.

Do not have access to those.

I think it is something with my web host. I created the code below, and it does not work (See the table for response types). .htaccess code is the same as here. The URL does not change at all.

index.php

<?
echo "hello there!";
echo $_GET['account_id'];
?>

|URL|RESPONCE|
|-|-|-|
| domain.tld/index?account_id=id_10464141 | hello there!id_10464141|
| domain.tld/index | hello there|

Ah you have MultiViews enabled… I missed that before

Try with Options -MultiViews instead of Options +MultiViews

Nope. Top part of .htaccess now reads:

IndexIgnore * 
Options -Indexes

Options -MultiViews +FollowSymLinks

RewriteEngine On

RewriteBase /

#Remove .html and .php extension (THIS WORKS)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]

Filename: viewaccount.php

MULTI VIEWS OFF

URL Entered URL Loaded Code Responce
domain.tld/FOLDER-NAME/viewaccount.php?account_id=id_52213234 404 ERROR

MULTI VIEWS ENABLED

URL Entered URL Loaded Code Responce
domain.tld/FOLDER-NAME/viewaccount.php?account_id=id_52213234 domain.tld/FOLDER-NAME/viewaccount?account_id=id_52213234 hello there!id_522132