3

I have the following file/folder structure for my site:

index.php 
games.php 
/category-games 
/category-games/game-1.php 
/category-games/game-2.php

The file games.php is supposed to be a category homepage for /category-games/. Is there any way to make this page show when someone visits mysite.com/category-games/? I tried to put the page into the folder and called it index.php but I guess that's not working.

Probably need to do this via .htaccess. Anyone can help me with this? Right now if anyone tries to access mysite.com/category-games/its going straight to 404.

Cheers!

anubhava
  • 761,203
  • 64
  • 569
  • 643
Charles Ingalls
  • 4,521
  • 5
  • 25
  • 33
  • 1
    If you call it index.php it should work, but possible duplicate of: http://stackoverflow.com/questions/19322345/how-do-i-change-the-default-index-page-in-apache – Jesper Jul 05 '15 at 15:29
  • @Djip yeah, that is what I thought. But it doesn't work. Even if I call it index.php it still goes to 404. – Charles Ingalls Jul 05 '15 at 15:49

3 Answers3

5

Case 1: If /category-games/ has no .htaccess then place this rule in root .htaccess:

RewriteEngine On

RewriteRule ^category-games/$ games.php [L,NC]

Case 2: If there is a .htaccess inside /category-games/ then use this rule

RewriteEngine On

RewriteRule ^/?$ /games.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • That works! But it clashes with my RewriteRule that removes the .php extension from all files. `RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.php [NC,L]` Only if I remove the rewriteRule it works but I'd like to keep it. Do you have a work around? – Charles Ingalls Jul 05 '15 at 16:14
  • I guess I can just place your rule above the RewriteRule to remove .php extension. Is that good practice? At least it works. – Charles Ingalls Jul 05 '15 at 16:16
  • 1
    Yes definitely this needs to be placed above your earlier rule. Ordering of rules is very important in .htaccess – anubhava Jul 05 '15 at 16:28
3

Well, if you are thinking about in a organize and systematic coding, then I would like to suggest you to use PHP Routing library. There are many ready mate routing classes available. If you want to use that, surely it will help you to make your code more organized way.

For example, if you want to access mysite.com/category-games/, behind the scene, routing will trigger your corresponding page.

Following code will try to access mysite.com/category-games folder but it will trigger out your-page.php file where user can see only mysite.com/category-games url, nothing else.

 $router->any('/category-games', function(){
    return 'your-page.php';
});

Isn't cool?

The list of routing library are-

Hope, will help you to do your project. TQ

tisuchi
  • 129
  • 2
  • 11
1

Try making a new page called index.html under /category-games/ with the following contents:

 <meta http-equiv="refresh" content="1;url=http://yoursite.com/category-games/games.php">

That would make index.html load by default but instantly redirect to games.php. The 1 is for how long to wait before redirecting. 1 is best so it doesn't overload your browser.

Toodles!

-HewwoCraziness

Eric Reed
  • 377
  • 1
  • 6
  • 21