URL rewrite with pattern based system used by web2pyref

Web2py has got two ways to rewrite URLs. First method is parameter-based system and second method is pattern-based system which is used by web2pyref.

Parameter-based system gives more flexibility but at the same time it is more complex to configure. Knowledge of regular expressions will make URL rewrite much easier to set up.

I have spent alot time reseaching and looking for examples on the Internet and this example is culmination of that search.

If you do not have time to learn regular epressions, you might find this example helpful.

Python /applications/appname/routes.py


Below you can find simplified version of web2pyref's routes.py configuration file located at the apps' root file.
Steps:
  • Copy below file to /home/username/web2py/applications/appname folder.
  • Replace 'web2pyref'  and/or domain name to your own application's name or domain name









































































































default_controller = 'pages' 

default_function = 'index'

routes_in = (
((
r'.*https?://www.web2pyref.com', r'.*https?://www.web2pyref.com/index')),
# Google domain checks
((r'.*https?://www.web2pyref.com.* /google123456789.html', r'/web2pyref/static/google123456789.html')),
# Needed for contact form
((r'.*https?://www.web2pyref.com.* /web2pyref/default/contact_form(?P<any>.*)', r'/web2pyref/default/contact_form.load')),
# Search functions
((r'.*https?://www.web2pyref.com.* /search(?P<any>.*)', r'/web2pyref/find/search\g<any>')),
# Tags Page
((r'.*https?://www.web2pyref.com.* /tags/(?P<any>.*)', r'/web2pyref/pages/show_page/tags/\g<any>')),
# Apps' download link
((r'.*https?://www.web2pyref.com.* /w2p/download/(?P<any>.*)', r'/web2pyref/references/download/\g<any>')),
# Tags functions
('/web2pyref/plugin_tagging/index/$anything', '/web2pyref/plugin_tagging/index/$anything'),
# Plugin Rate
('/web2pyref/plugin_rating/rate/$anything', '/web2pyref/plugin_rating/rate/$anything'),
# Comments functions
('/web2pyref/comments/$anything', '/web2pyref/comments/$anything'),

# do not reroute admin unless you want to disable it
#('/admin', '/admin/default/index'),
#('/admin/$anything', '/admin/$anything'),
# do not reroute appadmin unless you want to disable it
#('/$app/appadmin', '/$app/appadmin/index'),
#('/$app/appadmin/$anything', '/$app/appadmin/$anything'),
# Images' location
('/static/images/pages_content/$anything', '/web2pyref/static/images/pages_content/$anything'),
# Login functions
(r'/user(?P<any>.*)', r'/web2pyref/default/user\g<any>'),
(
r'/home(?P<any>.*)', r'/web2pyref/home\g<any>'),
# Static Files
('/static/(?P<any>.*)','/web2pyref/static/$any'),

(
'/index', '/web2pyref/pages/show_page/index'),

# do not reroute static files
('/$app/static/$anything', '/$app/static/$anything'),
# reroute favicon and robots, use exable for lack of better choice
('/favicon.ico', '/web2pyref/static/favicon.ico'),
(
'/robots.txt', '/web2pyref/static/robots.txt'),
# do other stuff

((r'.*https?://www.web2pyref.com.* /web2pyref/plugin_tagging(?P<any>.*)', r'/web2pyref/plugin_tagging\g<any>')),

((
r'.*https?://www.web2pyref.com.* /', r'/web2pyref/pages/show_page/index')),
((
r'.*https?://www.web2pyref.com.* (?P<any>.*)', r'/web2pyref/pages/show_page\g<any>')),

(
'/$anything', '/$anything')
)

routes_out = (
((
r'/web2pyref/pages/show_page/index', r'.*https?://www.web2pyref.com')),

((
r'.*https?://www.web2pyref.com.* /web2pyref/comments/example/(?P<any>.*)', r'/example/\g<any>')),
((
r'.*https?://www.web2pyref.com.* /web2pyref/comments/reference/(?P<any>.*)', r'/reference/\g<any>')),

## Display tags page properly
((r'.*https?://www.web2pyref.com.* /web2pyref/pages/show_page/tags/(?P<any>.*)', r'/tags/\g<any>')),

## Search page
(r'.*https?://www.web2pyref.com.* /web2pyref/find/search(?P<any>.*)', r'/search\g<any>'),
(
'/web2pyref/find/search','/search'),

## Download files from reference page
(r'.*https?://www.web2pyref.com.* /web2pyref/references/download/(?P<any>.*)', r'/w2p/download/\g<any>'),
# Plugin Favorite
('/web2pyref/plugin_favourite/$anything', '/web2pyref/plugin_favourite/$anything'),
# Plugin rate
('/web2pyref/plugin_rating/rate/$anything', '/web2pyref/plugin_rating/rate/$anything'),

(
'/web2pyref/comments/$anything', '/web2pyref/comments/$anything'),
# do not reroute admin unless you want to disable it
('/admin/$anything','/admin/$anything'),
# do not reroute appadmin unless you want to disable it
('/$app/appadmin/$anything', '/$app/appadmin/$anything'),

(
'/web2pyref/static/(?P<any>.*)','/web2pyref/static/$any'),
(
r'.*https?://www.web2pyref.com.* /web2pyref/pages/show_page/index', r'/'),
(
'/web2pyref/default/index','/'),

# do not reroute static files
('/$app/static/$anything', '/$app/static/$anything'),
(
r'/web2pyref/default/user(?P<any>.*)',r'/user\g<any>'),
(
r'/web2pyref/home(?P<any>.*)',r'/home\g<any>'),
(
r'/web2pyref/pages/edit_page(?P<any>.*)',r'/edit_page\g<any>'),

(
r'.*https?://www.web2pyref.com.* /web2pyref/pages/show_page(?P<any>.*)', r'\g<any>'),
(
r'/web2pyref/events/edit_event(?P<any>.*)',r'/edit_event/\g<any>'),
#Examples and Refs
(r'.*https?://www.web2pyref.com.* /web2pyref/references/show_reference(?P<any>.*)', r'/reference\g<any>'),
(
r'.*https?://www.web2pyref.com.* /web2pyref/examples/show_example(?P<any>.*)', r'/example\g<any>'),

(
r'.*https?://www.web2pyref.com.* /web2pyref/plugin_tagging(?P<any>.*)', r'/web2pyref/plugin_tagging\g<any>'),
(
r'/web2pyref/plugin_tagging/(?P<any>.*)',r'/web2pyref/plugin_tagging/\g<any>'),

(
'/$anything', '/$anything'),
)

routes_onerror = [
(
r'*/404', r'/web2pyref/static/404.html')
]

Python /routes.py


  • Create or edit file routes.py located at the web2py root folder and add code below.
  • Reload routes from the admin page or restart the web2py server.





















# -*- coding: utf-8 -*-

# default_application, default_controller, default_function
# are used when the respective element is missing from the
# (possibly rewritten) incoming URL
#

default_application = 'web2pyref' # ordinarily set in base routes.py

default_controller = 'default' # ordinarily set in app-specific routes.py
default_function = 'index' # ordinarily set in app-specific routes.py
# routes_app is a tuple of tuples. The first item in each is a regexp that will
# be used to match the incoming request URL. The second item in the tuple is
# an applicationname. This mechanism allows you to specify the use of an
# app-specific routes.py. This entry is meaningful only in the base routes.py.
#
# Example: support welcome, admin, app and myapp, with myapp the default:


routes_app = ((r'/(?P<app>admin|web2pyref)', r'\g<app>'),
(
r'(.*)', r'web2pyref'),
(
r'/?(.*)', r'web2pyref'))

Result



loading...

Comments

loading...

Sources / Reference

" Web2py Book - Chapter 4 - The Core" is licensed under Creative Common License BY-NC-ND 3.0

Tag Cloud

accepted Acces Control access rights actions_disabled admin allowed_attributes allowed_schemes App apps args auth authentication B bar chart BEAUTIFY begin BODY BR carousel CAT CENTER chart check CLEANUP client request client_side CMS CODE COL COLGROUP comment compute contact form crud.create crud.delete crud.read crud.select crud.tables CRUD custom attributes custom form DAL data define_table deletable depriciated DIV doctype dot dspval EM embed64 enable_record_version end error_message example extension Field types Field FIELDSET file filename form.accepted form.accepts form.process form.validate FORM format Forms Grid H1 H2 H3 H4 H5 H6 hash_vars Hello helper helpers highchart hmac_key host hosting HTML image images inpval ip address ip IS_DATETIME IS_DATETIME_IN_RANGE IS_DECIMAL_IN_RANGE IS_EMPTY_OR IS_EQUAL_TO IS_EXPR IS_FLOAT_IN_RANGE IS_IMAGE IS_INT_IN_RANGE IS_IN_DB IS_IN_SET IS_IPV4 IS_LIST_OF IS_NOT_EMPTY IS_NOT_IN_DB IS_NULL_OR is_slug IS_STRONG IS_TIME IS_UPLOAD_FILENAME IS_UPPER IS_URL lable lambda lang lastdot linkto li_class li_first li_last login_methods maximum maxip maxlen MENU min minimum minip mobile mode module multiple permitted_tags pie chart placeholder plugin prepend_scheme pythonanywhere query rating reCaptcha redirect request.now routes.py salt sanitize scheme services settings slug sortable special SQLFORM() SQLFORM.grid SQLFORM SSL submit table constructor TAG test text TinyWebsite ul_class update upper URL rewrite URL validator value vars Views widget XML xmlescape _class _common_fields


Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)