Reputation: 119
I have a website *https://www.example.com. I want to open home, category, and sub-category dynamically like:
https://www.example.com
https://www.example.com/education
https://www.example.com/education/PHP-Training
I have codded index.php like this:
if(isset($_GET['category'])){
$category = $_GET['category'];
$subCategory = $_GET['subcategory'];
include('files/categoryFile.php');
}else{
include('files/indexFile.php');
}
categoryFile.php
will use the above two variables $category
, $subCategory
to get the data from the database and indexFile.php
is static file for index page.
Below is the code of .htaccess
for the rewrite rule to do the same:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+) index.php?category=$1&subcategory=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?category=$1 [NC,L]
It works alright here but the problem is, it also rewrite https://www.example.com/css/style.css
to the same rule. I can stop it from rewriting these particular folders by using dropping .htaccess
file to this folder with below code:
<IfModule mod_rewrite.c>
#Disable rewriting
RewriteEngine Off
</IfModule>
But it doesn't look like a good solution to me. I want to know if there is a better solution for this. If this question doesn't match the question criteria. Please comment it and I will delete the question. Also, suggest an edit if there could be a change. Thanks
Upvotes: 0
Views: 213
Reputation: 1293
I have done this thing in one of my project. You have to add one condition to before your rewrite rule and one more thing that is you should create assets folder so you can store there all public files and folders and you can make public using one rule.
Check this solution according your example.
RewriteCond %{REQUEST_URI} !/css
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+) index.php?category=$1&subcategory=$2 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?category=$1 [NC,L]
I think you just need to add a couple more conditions to exclude them.
RewriteCond %{REQUEST_URI} !/css
RewriteCond %{REQUEST_URI} !/js
RewriteCond %{REQUEST_URI} !/images
Upvotes: 1