JavaScript or PHP to Manipulate .htaccess File

Hello there Wapplers…

Do any of you use a JavaScript or PHP function to create and manage routes in your .htaccess file?

Is that possible?

I want to automatically generate simple routes to more complex urls that have query parameters…

Thanks!
Antony.

You can create routes with multiple, dynamic parameters:

image

I would be surprised if dynamically modifying the .htaccess file would be the best approach (in any situation).

Tom

1 Like

Thanks @TomD!

I’m no expert in .htaccess files and routing, so I’m curious as to why you consider dynamically adding to the file is a bad idea?

Usually you define your routes and then just assign different slug and id to them to make different “pages”. Example, you have a hotel booking site and the hotel details page looks like:

https://mysite.com/hotel.php?id=123&name=great+hotel

then you make routing to it which looks like:

/hotel/:id/:name/

and then your users can create different “pages” using routing and you can allow them entering different names slugified like:

https://mysite.com/hotel/1/great-hotel
https://mysite.com/hotel/2/hotel-name-here
https://mysite.com/hotel/99/some-name

so the “dynamic” part here is the slug and the id. The routing path is all the same.

1 Like

It’s not exactly that it’s a bad idea - but it’s not necessary (at least, I’ve never found it necessary).

Hopefully, @Teodor’s answer has clarified this anyway.

1 Like

Thanks for that @Teodor!

So my question is how to let my users do what you suggest for the booking forms that they create with my app when they don’t have access to Wappler!

Hi Antony

Your users will just be storing data in your database. Your routing uses that data to create the URL paths.

So if you had a field in your database for username, and your users edit that field, your routing could be set like this:

Path: /userdetails/:username
URL: /userdetails.php

So they would then go to /userdetails/sitestreet (for example) and see that page with all the data pulled in via the GET variable username.

Like this:

4 Likes

Okay, it starts to make sense now… thank you everyone, I will have a play!

1 Like

@Antony When I was handcoding, I built a CMS that would allow creation of pages so it was necessary for me to dynamically update the .htaccess file. I found this in some very old code of mine. May be useful to you if you modify it for your needs for use in Wappler.

#################################
### CHECK FOR .HTACCESS FILE
#################################
$htaccess = './.htaccess';
if (!file_exists($htaccess)) {//if file doesn't exist or install path changed
$fh = fopen($htaccess, 'w') or die("can't open htaccess file");
$stringData = "<IfModule mod_rewrite.c>\r\n
Options +FollowSymlinks\r\n
RewriteEngine On\r\n
RewriteBase ".$_SESSION['path']."\r\n
RewriteCond %{REQUEST_FILENAME} !-f\r\n
RewriteCond %{REQUEST_FILENAME} !-d\r\n
##home login mypage etc\r\n
##RewriteRule ^([^/\.]+)/?$ index.php?pg_ident=$1 [QSA,L]\r\n
##home.html login.html mypage.html etc\r\n
##pg_ident cannot be page_ident otherwise submit_value() if always the _GET value\r\n
RewriteRule ^([^/\.]+)".$_SESSION['file_extension']."$ index.php?pg_ident=$1 [QSA,L]\r\n
</IfModule>\r\n";
fwrite($fh, $stringData);
fclose($fh);
}//if (!file_exists('.htaccess')) {
#################################
### END CHECK FOR .HTACCESS FILE
#################################

-Twitch

Thank you soooo much @TwitchBlade! :tada:

1 Like