Friendly URLs .htaccess

Using .htaccess we can have “friendly” URLs

That meaning if a visitor goes to yoursite.com/guestbook it will load the content of yoursite.com/guestbook.php/html depending on the choice picked.

There are various other methods to do this also, if you’ve got a favourite feel free to add below! :slight_smile:

Removal of PHP extension via .htaccess

# 000webhost Fire up PHP file without filename extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php

Removal of HTML extension via .htaccess


# 000webhost Fire up HTML file without filename extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html

The same code can be modified to use for any file i.e. .htm :slight_smile:

If we don’t want users to access yoursite.com/guestbook.php at all then we would also insert below the above code

```
# Fire up 404 if original request is .php 000webhost
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
```

The same for if you didn’t want the visitor to access yoursite.com/guestbook.html and instead wants a 404 to display you can use

```
# Fire up 404 if original request is .html 000webhost
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.html[? ].*$"
RewriteRule .* - [L,R=404]
```

.HTM/ any other extension just edit as required.

```
# Fire up 404 if original request is .htm 000webhost
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.htm[? ].*$"
RewriteRule .* - [L,R=404]
```
1 Like