Friday, March 23, 2012

htaccess python script, Clean URL

I enjoy the clean URL when it looks like http://baligena.com/home/page

However every time I create a new app I have to re-edit/create a htaccess file. This is too much work and remembering. Below is a python script for windows and linux that once ran inside the root folder of your site it will create an htaccess file to make that clean URL you always wanted. You can also fork it at GitHub

Warning! You may need to configure apache and php for the htaccess file to work. Click me for instructions.

createhtaccess.py



# this script customizes and creates the .htaccess to correspond to the current directory

import os

def create_htaccess():
f = open('./.htaccess','w')
# get directory path
directory = os.getcwd()

# if linux
if os.name == 'posix':
dirname = directory[directory.rfind('/')+1:]
# if windows
elif os.name == 'nt':
dirname = directory[directory.rfind("\\")+1:]
else:
exit('error occurred: couldnt determine the system this computer is on')



f.write('''
# http://codeigniter.com/wiki/mod_rewrite
# make sure you change the root url on line 27 RewriteRule ^(.*)$ directory/index.php?/$1 [L]


RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ '''+ dirname + '''/index.php?/$1 [L]



# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php

''')
f.close()

if __name__ == '__main__':
create_htaccess()



No comments:

Post a Comment