How to get rid if the /init/default/ in the URL?

333 views
Skip to first unread message

Joe

unread,
Sep 15, 2017, 2:43:11 AM9/15/17
to web2py-users
I've been trying to do this for so long, but with pythonanywhere hosting, it doesn't seem to be possible to get rid of the init/default/ part of the URL.

I think, this nested sub-directories structure is not optimal for SEO and also makes it difficult to place the robot,txt and sitmap.xml in a way that it's easy to find for search engines?
I am not a SEO expert, but I read that this kind of URL structure is not optimal.

When I looked at the Web2py website, which I think is also hosted by pythonanywhere, I noticed that, www.web2py.com has not solved this problem either i.e. http://www.web2py.com/init/default/support

Is there any way to change this URL structure?

Anthony

unread,
Sep 15, 2017, 9:45:19 AM9/15/17
to web2py-users
See http://web2py.com/books/default/chapter/29/04/the-core#Parameter-based-system. In routes.py, something like:

routers = dict(
    BASE
= dict(
        default_application
='init',
        default_controller
='default',
        default_function
='index'
   
),
    init
= dict(
        functions
=['list', 'of', 'functions', 'in', 'default', 'controller']
   
),
)

Anthony

Joe

unread,
Sep 16, 2017, 11:36:34 PM9/16/17
to web2py-users
Hello Anthony,

Thanks very much for your reply.

Yes, I tried this in the routes,py file in the web2py folder on Pythonanywhere and tried several other ways from the link you posted but nothing changed in the URL.

I think even this should work too, but it doesn't:

routers = dict(
  BASE  = dict(default_application='myapp'),
)

I must be missing something.

Joe

Anthony

unread,
Sep 17, 2017, 9:05:05 AM9/17/17
to web2py-users
Did you reload routes or restart the server?

Joe

unread,
Sep 17, 2017, 9:24:31 AM9/17/17
to web2py-users
Yes, I reloaded the app. Nothing happens, it doesn't change the URL.

I tried several different ways to change the routes.py.

In the current situation in the web2py folder I have this in the routes.py file: 

routers = dict(
  BASE  
= dict(default_application='init'),
)

Then, in the web2py/applications/myappfolder/applications/init/routes.py I have the same code:

routers = dict(
  BASE  
= dict(default_application='init'),
)


Something doesn't seem right about the above path and folder structure. Maybe this is the problem, but this seems to be the only way to install the app on Pythonanywhere. But, this doesn't seem right.

Anthony

unread,
Sep 17, 2017, 10:39:46 PM9/17/17
to web2py-users
First, you can just put all the router code in /web2py/routes.py -- no need for anything specific in the app folder. Second, you need to specify the functions in the default controller if you want /default/index to be gone when there are any request.args.

How are your URLs being generated? What happens if you just go to http://yourdomain.com?

Anthony

Joe

unread,
Sep 18, 2017, 9:30:26 AM9/18/17
to web2py-users
Thanks Anthony, when you say "specify the functions in the default controller ", what do you mean exactly? I just want to make sure I understand you correctly.

When I go to  http://mydomain.com I don't have the  init/default/ in the URL. But, when I click to go to an other page I do have the  init/default/ in every URL, even when I go back to the  http://mydomain.com it will now contain the init/default/. 
So, yes it is when I am requesting args, I guess.

Can you walk me through how to specify the functions in the default controller?

Anthony

unread,
Sep 18, 2017, 10:05:31 AM9/18/17
to web2py-users
On Monday, September 18, 2017 at 9:30:26 AM UTC-4, Joe wrote:
Thanks Anthony, when you say "specify the functions in the default controller ", what do you mean exactly? I just want to make sure I understand you correctly.


But that does not appear to be the issue you are experiencing. You might have to contact Pythonanywhere support, as it sounds like somehow it is ignoring the routes.py file.

Anthony

Anthony

unread,
Sep 18, 2017, 10:25:54 AM9/18/17
to web2py-users
FYI, here is a Pythonanywhere app with routing working as expected (the app is named "init"): https://w2ptest.pythonanywhere.com/

Below are the exact contents of the /web2py/routes.py file for the above installation:

routers = dict(
    BASE
= dict(
        default_application
='init'
   
),

    init
= dict(
        default_controller
='default',
        default_function
='index',
        functions
=['call', 'download', 'index', 'user']
   
)
)

Also, note that as long as you are going to use the rewrite system as above, there is no particular reason to name your app "init". The only reason to use "init" is if you are not using the rewrite system and want a default application when accessing a URL with the domain name only.

Anthony

Joe

unread,
Oct 13, 2017, 5:12:13 AM10/13/17
to web2py-users
Thanks very much Anthony for all your help with this one. I copied the exact code you posted but nothing changed.

I wonder if this has to do with having multiple apps in the web2py directory. I just can't fix this issue no matter what I do.

Btw, even if I was to manage somehow to get rid off the "init", is it possible to have this URL mysite.com/robot.txt  - probably not possible, right? Even on the link you sent, it would be mysite.com/default/robot.txt - which means the search engines wouldn't find it.

But, I can't even get rid of the "init", which is frustrating. I asked about this on pythonanywhere, but they don't know. They say it's a web2py issue.

Dave S

unread,
Oct 13, 2017, 2:35:11 PM10/13/17
to web2py-users


On Friday, October 13, 2017 at 2:12:13 AM UTC-7, Joe wrote:
Thanks very much Anthony for all your help with this one. I copied the exact code you posted but nothing changed.

Did you restart web2py?  The routes file isn't normally reloaded while the server is running.

I wonder if this has to do with having multiple apps in the web2py directory. I just can't fix this issue no matter what I do.

Btw, even if I was to manage somehow to get rid off the "init", is it possible to have this URL mysite.com/robot.txt  - probably not possible, right? Even on the link you sent, it would be mysite.com/default/robot.txt - which means the search engines wouldn't find it.


Put robots.txt in your static folder.

/dps

 

Joe

unread,
Oct 13, 2017, 7:22:42 PM10/13/17
to web2py-users
Hi Dave, I have reloaded the apps in web2py if that's what you mean. I can't restart web2py, I can only reload the apps, I guess.
Is there an other way on pythonanywhere? I mean, restarting web2py instead of reloading the apps?

Re: robots.txt
If I put robots.txt in my static folder the URL will still have to be mysite.com/init/static/robots.txt to reach it- this is the issue I am trying to solve - I am trying have mysite.com/robots.txt

Dave S

unread,
Oct 14, 2017, 2:15:21 AM10/14/17
to web2py-users


On Friday, October 13, 2017 at 4:22:42 PM UTC-7, Joe wrote:
Hi Dave, I have reloaded the apps in web2py if that's what you mean. I can't restart web2py, I can only reload the apps, I guess.
Is there an other way on pythonanywhere? I mean, restarting web2py instead of reloading the apps?

I'm not sure what you mean by "reload the apps".  Routes.py in the web2py directory is normally read-once.

I haven't used pythonanywhere, and my Heroku experience is with node.js, and a few months ago.  As I recall, with Heroku you stop the server from the Heroku dashboard.  You then do a normal reload, which will read the git repo again, pour everything into a fresh container, and start the server.  If pythonanywhere isn't like that, then you'll have to wait for someone with relevant experience to comment.  I think that Massimo hosts the documentation (aka the example app) on pythonanywhere, but you'll have to wait for his confirmation of that.


Re: robots.txt
If I put robots.txt in my static folder the URL will still have to be mysite.com/init/static/robots.txt to reach it- this is the issue I am trying to solve - I am trying have mysite.com/robots.txt


In my cloud instance (which is native AWS linux), I have a routes.py that sets the default app (magic4.myemployer.com, so to speak) and have the robots.txt in myapp/static.  I have confirmed with curl from another machine that that works as desired.

Good luck!

/dps

 

Anthony

unread,
Oct 15, 2017, 10:05:08 AM10/15/17
to web2py-users
On Friday, October 13, 2017 at 5:12:13 AM UTC-4, Joe wrote:
Thanks very much Anthony for all your help with this one. I copied the exact code you posted but nothing changed.

I wonder if this has to do with having multiple apps in the web2py directory.

No, the above example also includes multiple apps.
 
Btw, even if I was to manage somehow to get rid off the "init", is it possible to have this URL mysite.com/robot.txt  - probably not possible, right?

Sure, just set the root_static key in the router, as described here. Actually, you don't have to bother setting that explicitly, as "robots.txt" is already set to be served from the root URL (note, it is "robots.txt", not "robot.txt").
 
But, I can't even get rid of the "init", which is frustrating. I asked about this on pythonanywhere, but they don't know. They say it's a web2py issue.

Hard to say what the problem is, but https://w2ptest.pythonanywhere.com/ demonstrates that the desired routing can be made to work on PythonAnywhere. Maybe start from scratch with a whole new PythonAnywhere web app or account.

Anthony

Anthony

unread,
Oct 15, 2017, 10:07:51 AM10/15/17
to web2py-users
On Friday, October 13, 2017 at 7:22:42 PM UTC-4, Joe wrote:
Hi Dave, I have reloaded the apps in web2py if that's what you mean. I can't restart web2py, I can only reload the apps, I guess.
Is there an other way on pythonanywhere? I mean, restarting web2py instead of reloading the apps?

Re: robots.txt
If I put robots.txt in my static folder the URL will still have to be mysite.com/init/static/robots.txt to reach it- this is the issue I am trying to solve - I am trying have mysite.com/robots.txt

Are you sure you have created /web2py/routes.py as described above and either reloaded the routes via the button in the web2py admin app or reloaded the web app via the PythonAnywhere "Web" tab? Note that routes.py must be in the root /web2py folder.

Anthony

Joe

unread,
Oct 23, 2017, 11:00:29 PM10/23/17
to web2py-users
Hi Anthony,

My file structure on pythonanywhere looks like this:


/home/username/web2py/applications/my_app_directory/applications/init

init being the app name.

 

In the web2py directory, I have a routes.py file which contains this code:

 

 

routers = dict(

 

    BASE
= dict(

        default_application
='init'

   
),

    init
= dict(

        default_controller
='default',

        default_function
='index',

        functions
=['call', 'download', 'index', 'user']

   
)

)



So, the web2py directory contains an application directory which contains all my app directories. Then, all the app directories also have an application directory which contains the app, named init.

 

I am not so sure if this is the optimal structure. Is it?

 

In any case, I used the button on the pythonanywhere Web tab to Reload www.myapp.com. And, nothing changed.

 

I would love to resolve this, and find out what's wrong. 


Thanks again for all your help.


Cheers,


Joe

Dave S

unread,
Oct 24, 2017, 1:27:22 AM10/24/17
to web2py-users


On Monday, October 23, 2017 at 8:00:29 PM UTC-7, Joe wrote:
Hi Anthony,

My file structure on pythonanywhere looks like this:


/home/username/web2py/applications/my_app_directory/applications/init

init being the app name.

 


The book doesn't mention using extra levels of directories when discussing PythonAnywhere deployment
(chapter 13).  Perhaps they are confusing the routing code when applying the "hide the app+controller" rules.

In the web2py directory, I have a routes.py file which contains this code:

 

 

routers = dict(

 

    BASE
= dict(

        default_application
='init'

   
),

    init
= dict(

        default_controller
='default',

        default_function
='index',

        functions
=['call', 'download', 'index', 'user']

   
)

)



So, the web2py directory contains an application directory which contains all my app directories. Then, all the app directories also have an application directory which contains the app, named init.

 

I am not so sure if this is the optimal structure. Is it?


Are you doing this for source control reasons?  I would instead consider using just the normal level of directories.  If you're dealing with multiple repositories, than maybe an ln -s might help flatten web2py's view while keeping trees separate, but I would try without it .

 

In any case, I used the button on the pythonanywhere Web tab to Reload www.myapp.com. And, nothing changed.

 

I would love to resolve this, and find out what's wrong. 


Thanks again for all your help.


Cheers,


Joe



/dps
 

Joe

unread,
Oct 24, 2017, 6:37:26 AM10/24/17
to web2py-users
Hi Dave,

Thanks for your reply.

No, I am not using this directory structure for source control reasons. I guess, I was a little bit confused when I initially set up the account.
So, this is a mess. 

What would be the easiest way to fix this? I wonder if I have to start from scratch with the account. I mean setting up everything again. I hope not.

Please let me know your thoughts.

Thanks again.

Cheers,

Joe

Joe

unread,
Nov 1, 2017, 9:31:53 PM11/1/17
to web2py-users
Hi Anthony,

It seems my problem is the directory structure setup on pythonanywhere.

But, I just followed the pythonanywhere system of installing web2py apps, I didn't actually create the directory structure myself.

Btw, on the web2py.com website I see a similar problem. When you first go to the web2py site you land on this URL: http://web2py.com/ then after you visit an other page and then go back to the index page, the URL changes to this:http://web2py.com/init/default/index
When I create a sitemap, these 2 URLs show up as 2 different pages for the same page. I think, this is considered as duplicate content by the search engines. 

In any case, can you advise me on how to fix this problem? My directory structure is clearly wrong:
/home/username/web2py/applications/my_app_directory/applications/init

Can you please post how the correct structure should look like on pythonanywhere?

Thanks again for all your help with this.

Cheers,

Joe

On Sunday, October 15, 2017 at 10:07:51 PM UTC+8, Anthony wrote:

Anthony

unread,
Nov 1, 2017, 10:05:13 PM11/1/17
to web2py-users
In any case, can you advise me on how to fix this problem? My directory structure is clearly wrong:
/home/username/web2py/applications/my_app_directory/applications/init

Can you please post how the correct structure should look like on pythonanywhere?

It should just be the usual folder structure:

/web2py/applications/init

Anthony

Anthony

unread,
Nov 1, 2017, 10:07:46 PM11/1/17
to web2py-users
Btw, on the web2py.com website I see a similar problem. When you first go to the web2py site you land on this URL: http://web2py.com/ then after you visit an other page and then go back to the index page, the URL changes to this:http://web2py.com/init/default/index

It's possible that there is no routes.py set up for the web2py.com site.

Anthony

Joe

unread,
Nov 2, 2017, 5:54:41 AM11/2/17
to web2py-users
In pythonanywhere you have to create a folder first before you install the app.
So you mean, it should be /web2py/applications/my_app_directory/init

Anthony

unread,
Nov 2, 2017, 11:20:41 AM11/2/17
to web2py-users

On Thursday, November 2, 2017 at 5:54:41 AM UTC-4, Joe wrote:
In pythonanywhere you have to create a folder first before you install the app.
So you mean, it should be /web2py/applications/my_app_directory/init

No -- the folder structure is:

/web2py
    /applications
        /init

Same on Pythonanywhere as on any other system. Below is a screenshot of the folder structure of this test app: http://w2ptest.pythonanywhere.com/.

Anthony

Joe

unread,
Nov 2, 2017, 9:47:35 PM11/2/17
to web2py-users
Yes, this is how the structure should look like, but it seems pythonanywhere doesn't allow me to setup the structure this way.

If I try to use this path to install a new app: 
/home/username/web2py/applications


I get this pythonanywhere error msg during the process:
Enter the path for a new directory to contain the web2py code
failed to remove
‘/home/my_user_name/web2py/applications’: Directory not empty


This has been my problem from the begging. Because of this error, the only way pythonanywhere allows me to install a new app is to create an new, empty directory first here:
/home/username/web2py/applications


So, that's how I end up with this problem:
/home/username/web2py/applications/new_directory/applications/init


Anthony

unread,
Nov 3, 2017, 7:31:00 AM11/3/17
to web2py-users
On Thursday, November 2, 2017 at 9:47:35 PM UTC-4, Joe wrote:
Yes, this is how the structure should look like, but it seems pythonanywhere doesn't allow me to setup the structure this way.

If I try to use this path to install a new app: 
/home/username/web2py/applications


I get this pythonanywhere error msg during the process:
Enter the path for a new directory to contain the web2py code
failed to remove
‘/home/my_user_name/web2py/applications’: Directory not empty

What do you mean by "install a new app"? If you want to create a new app, you can either use the web2py admin interface, or you can just go to the Pythonanywhere "Files" tab in the dashboard, navigate to the /web2py/applications directory, and enter a new directory name where it says "Enter new directory name" (or you can just use a Bash console to create a folder).

If none of that is working for you, either contact Pythonanywhere support or just create a new account.

Anthony

Alexander

unread,
Jun 22, 2018, 2:10:36 PM6/22/18
to web2py-users

You can solve this probem by adding the sitemap.xml and the robots.txt to the routes.examply.py in /home/your_app_name/application/init/routes.example.py

routes_in=(
  ('.*:/favicon.ico','/init/static/favicon.ico'),
  ('.*:/robots.txt','/init/static/robots.txt'),
  ('.*:/sitemap.xml','/init/static/sitemap.xml'),

)
routes_out = ()

This had worked for me.

Maurice Waka

unread,
Aug 31, 2019, 7:58:49 AM8/31/19
to web2py-users
Hi
This is a very good discussion.
I followed it and it worked for me.
However, I have multiple controllers with different views, How can I make it (HIDE ALL CONTROLLER NAMES) for all pages.
So far it only works for default/index
Kind regards

Dave S

unread,
Sep 3, 2019, 3:06:45 AM9/3/19
to web2py-users


On Saturday, August 31, 2019 at 4:58:49 AM UTC-7, Maurice Waka wrote:
Hi
This is a very good discussion.
I followed it and it worked for me.
However, I have multiple controllers with different views, How can I make it (HIDE ALL CONTROLLER NAMES) for all pages.

Say what?  How do you distinguish between apps and pages if you are going to get take the controller names out of the URLs?  You can use routes.py to map everything to a default path, but then thats what you get .. the default path.

You can abuse this to make myapp/default/showstuff look like ,myapp/index?showstuff, but then you just make index figure out what the real functions are.

/dps

Lovedie JC

unread,
Sep 3, 2019, 9:11:50 AM9/3/19
to web...@googlegroups.com
Ok. I meant hide them.
Regards 

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to the Google Groups "web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/c21f4800-bedb-41c1-818d-29d9babb9d8c%40googlegroups.com.

Maurice Waka

unread,
Sep 26, 2019, 6:06:56 AM9/26/19
to web2py-users
I think this is what I wanted.

routers = dict(
    BASE=dict(
        default_application='myapp',

        default_controller='default',
        default_function='index',

        functions=dict(
            default=['list', 'of', 'functions', 'in', 'default.py'],
            foo=['list', 'of', 'functions', 'in', 'foo.py']
        )
    ),
    admin=dict(
        default_function='site',
        functions=['list', 'of', 'functions', 'in', 'default.py']
    )
)
However trying it out on the local host does not change the name.

I also used this in the routes.py file with no change in the name

routes_in = (
    ('/testme', '/examples/default/index'),
)
routes_out = (
    ('/examples/default/index', '/testme'),
)
Reply all
Reply to author
Forward
0 new messages