Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an index page in a subfolder

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!

like image 706
Charles Ingalls Avatar asked Nov 27 '25 20:11

Charles Ingalls


2 Answers

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]
like image 188
anubhava Avatar answered Nov 29 '25 16:11

anubhava


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-

  • https://github.com/chriso/klein.php
  • https://github.com/nikic/FastRoute

Hope, will help you to do your project. TQ

like image 24
tisuchi Avatar answered Nov 29 '25 15:11

tisuchi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!