Removing PHP file extension when working with PHP files

I am trying to remove a file extension in an address from all files *.php

on my site. For example, if the user visits mysite.com/about.php

, I want the url to read mysite.com/about

.

Here's mine app.yaml

:

application: mysite
version: 1
runtime: php55
api_version: 1

handlers:
# Static files
- url: /images/*
  static_dir: images
- url: /css/*
  static_dir: css
- url: /js/*
  static_dir: js

# Routing
- url: /services(.*)
  script: services.php
- url: /portfolio(.*)
  script: portfolio.php
- url: /project(.*)
  script: project.php
- url: /about(.*)
  script: about.php
- url: /contact(.*)
  script: contact.php
- url: /(.*)
  script: index.php

      

How can I achieve this in GAE?

+1


source to share


1 answer


If all you have to do is do this type of routing, you can use this:

application: mysite
version: 1
runtime: php55
api_version: 1

handlers:
# Static files
- url: /images/*
  static_dir: images
- url: /css/*
  static_dir: css
- url: /js/*
  static_dir: js

# Special case, route requests for the root document
- url: /
  script: /index.php
# If the request ends in [something].php, route to that file directly
- url: /(.+\.php)$
  script: /\1
# Map all other requests to scripts of the same name
# so that, eg, /thispage (or /thispage/) routes to /thispage.php
- url: /(.*?)/?$
  script: /\1.php

      

NB, however, using this method, you won't be able to handle 404 errors (although they don't seem to be handled in the app.yaml app mentioned in the question). The error handlers in your app.yaml won't work for 404 because they will only work if they can't match whatever route you provide.

So, if you want to handle 404 errors, etc., then you need to do the routing inside a php script instead, like this:



-url: /(.*)
 script: /routes.php

      

and inside routes.php check the $ _SERVER ['REQUEST_URI'] variable to see which page was requested and serve the appropriate content accordingly.

edit: thanks to @Avinash Raj for cleaning up the regex Here

+2


source







All Articles