0

Trying to build a website (not wordpress), using php... want to override .htaccess file for making Permalinks. I wrote the following, but it doesn't works. I use utf-8 encoding. Hosting on NameCheap hosting. Please help.

RewriteEngine On
RewriteRule ^laptops/$ https://example.com/categories.php?category=$1 [R=301,L]

From the Main file, I am calling categories.php, using the following link:

<a href="/categories.php?category=<? echo strtolower($row1["subcatname"]); ?>" class="card-link" ><? echo $row1["subcatname"]; ?> </a>

Categories.php has the following code:

<?php
    if(isset($_GET['category']) && $_GET['category'] == 'laptops') {
        echo $_GET['category'] ;
    }
?>

.htaccess has the following script:

RewriteEngine On
RewriteRule ^laptops/$ https://onlibest.com/categories.php?category=$1 [R=301,L]

My expectation : https://example.com/categories.php?category=laptops, should be shown to the user as following Permalink https://example.com/laptops

1 Answers1

1

First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.

Then, put this code in your htaccess

RewriteEngine On
RewriteRule ^([^/\.]+)/?$ categories.php?category=$1 [L]

Should work for :

example.com/categories.php?category=laptops

to

example.com/laptops 
Amine KOUIS
  • 1,686
  • 11
  • 12
  • I checked that already and htaccess is readable... I had tried this script before also, but it doesn't works... Does it have anything to do with encoding ? I use utf-8 encoding for saving .htaccess.... (just a small info. I once changed the encoding, and that corrupted the file. Later I changed back to utf-8)... not sure if that is causing the issue... – Gurpreet Singh Sep 07 '20 at 13:47
  • Do you have mod_rewrite enabled? – Amine KOUIS Sep 07 '20 at 14:18
  • How do I check that on a Shared Hosting ? I couldn't locate httpd.conf file on their file server – Gurpreet Singh Sep 07 '20 at 14:22
  • When you request the url https://example.com/laptops what you get? – Amine KOUIS Sep 07 '20 at 22:54
  • Check this link https://stackoverflow.com/questions/9234289/how-to-debug-htaccess-rewriterule-not-working it may help you – Amine KOUIS Sep 07 '20 at 22:56
  • I get the following URL : https://example.com/categories.php?category=laptops – Gurpreet Singh Sep 08 '20 at 05:40
  • I checked the link : however, as I said I am making a website on a Shared Hosting, where we have shared resources with others. I don't have dedicated server. I can't restart apache etc. – Gurpreet Singh Sep 08 '20 at 05:44
  • @GurpreetSingh ` – MrWhite Sep 09 '20 at 18:48