Search code examples
php.htaccessmod-rewriteurl-routing

Showing the same page to a user but change the content based on the URL?


I'm working on a site where for SEO reasons I need to have around 50 pages all with the same content, but with certain words swapped out based on the URL. The certain words are all locations (i.e UK, Spain, France etc). So the content on the page will say "personal training courses UK or personal training courses Spain, personal training courses France).

Currently I have it so the URL is www.websitename.com/courses.php?location=uk. Then on the courses.php page I'm doing a $_GET to get the location parameter and then changing the content of the page based on this. Ideally what I really want is for the URL to be www.websitename.com/courses/uk, www.websitename.com/courses/spain mainly for aesthetic reasons. Is there a rewrite rule to achieve this? I'm also open to alternatives so I can get the location in the URL using other methods to $_get.


Solution

  • There are two ways of accomplishing this.

    1. Use a router

    FastRoute and Klein.php are good choices.

    1. .htacess rewrites

    You can modify your .htaccess file to rewrite the URL for you. Effectively showing the user a different URL to what your application needs.

    Options +FollowSymLinks  
    RewriteEngine On 
    
    RewriteRule ^courses/([a-zA-Z]+)$ courses.php?location=$1 [L,NC]