bookmark_borderHow to remove .php / .html extension from URL slug

Was recently working on a project, not using a CMS like WordPress and Daniel was making fun of me for requiring “.php” in my URL.

Naturally, that could not go without a response.

Example of What I wanted to Do

Example of what I’m talking about:

https://defragged.org/somdirectory/somefile.php

I wanted to remove the “.php” extension from the URL, so that it reads:

https://defragged.org/somdirectory/somefile

If this is you, then here is the quick and dirty on how to get this done.

How To Remove .PHP / .HTML from URL Slug

I am working on Apache and the latest version of Ubuntu, but it should be the same for most of the latest versions.

Because this is a test server, I wanted to do a global deployment so that I don’t have to worry about it anymore. This saves me from having to remember to do this in each web directory via an .htaccess file.

Navigate to your apache config file, something like this:

# vim /etc/apache2/apache2.conf 

Scroll to the bottom, and append this to your file:

<Directory /var/www/>
 <IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)/$ $1 [R,L]
  RewriteCond %{REQUEST_FILENAME}.php -f
  RewriteRule (.*) $1.php [L]
  RewriteCond %{REQUEST_FILENAME}.html -f
  RewriteRule (.*) $1.html [L]
 </IfModule>
</Directory>

Save the file, and restart the web server.

service apache2 restart

That should be it, clear your browser cache, and revisit the site. It should do the trick.

Sharing is caring!

bookmark_borderParse JSON Responses using Bash Scripts

I was working with the categorify.org site and I wanted to parse through the API’s response. The response was in JSON format.

There are a number of different ways you can do this, but if you want a quick, simple, way that uses existing tools you probably already have installed, this is for you.

To parse through the JSON response I used Python.

Here is an example of what I was doing:

curl -s https://categorify.org/api?website=pornhub.com

If you do this, the response is something like this:

$ curl -s https://categorify.org/api?website=pornhub.com

{“domain”:”pornhub.com”,”ip”:”31.192.120.36″,”country-code”:”NL”,”country”:”Netherlands”,”rating”:{“language”:true,”violence”:false,”nudity”:true,”adult”:true,”value”:”R & NSFW”,”description”:”Adult-only content and not safe for a work environment”},”category”:[“Adult\/Pornography”],”keyword_heatmap”:{“videos”:99,”free”:85,”pornhub”:61,”porn”:55,”models”:54,”premium”:45,”content”:45,”rated”:44,”photos”:44,”exclusive”:43,”party”:43,”gifs”:42,”sister”:39,”discover”:36,”online”:36,”video”:35,”albums”:34,”pornhubcom”:33,”cancel”:33,”subscribed”:30}}

What I specifically wanted was the category value: “category”:[“Adult\/Pornography”]

So I modified my command to pipe my output to python and ran the following:

$ curl -s https://categorify.org/api?website=pornhub.com | python -c ‘import json,sys;obj=json.load(sys.stdin);print obj[“category”]’

The section you’re most interested in is this: print obj[“category”]

You have to choose which object you want to print. In my case, I wanted the category object as it holds the value I am looking for.

You can use this to parse any JSON response, just update the print object with one that corresponds to your results. The beautiful part is that this should be available on most major OS platforms without additional installations.


Side note, here are two variations depending on which version of Python you have on your machine.

Python 3

python3 -c "import sys, json; print(json.load(sys.stdin)['category'])"

Python 2

python2 -c "import sys, json; print json.load(sys.stdin)['category']"