0

I am stuck in a problem where the code redirects as expected but it is causing other urls to show 404.

Here is my htaccess code:

RewriteEngine On
RewriteBase /
RewriteRule ^category/(.*)$ catlinks.php?id=$1 [L] 
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ show.php?id=$1 [QSA,L]

WHen I change RewriteRule ^category/(.*)$ catlinks.php?id=$1 [L] to RewriteRule ^(.*)$ catlinks.php?id=$1 [L] it works as expected by removing category slug from url.

the above code changes the slug from domain.com/abc/xyz to: domain.com/xyz

THe category function is working as expected but this rule is causing other links on website to show 404

THe first thing I want:

RewriteRule ^(.*)$ show.php?id=$1 [QSA,L]

this redirects the show.php?id=$1 to domain.com/abc but with above category rule it is not working.

then, I have an admin page which is accessible by: domain.com/admin/login.php (admin slug remains in all pages of admin panel) but above code causes the admin panel to show 404

So, in short here is what I want:

  1. remove category slug from url which should show category as domain.com/abc whereas currently it is: domain.com/category/abc
  2. THe admin page should be accessible whenever I add /admin to domain and all its pages should work i.e. domain.com/admin/login.php, domain.com/admin/products.php
  3. The product page show.php?id=$1 should work fine and redirect to domain.com/abc

I am not a pro in htaccess so stuck in it.

Help please.

  • By removing the "category" from the URL you would seem to be creating a conflict... how do you know whether a request for `/abc` should be rewritten to `show.php?id=abc` or `catlinks.phpid=abc`? – MrWhite Apr 23 '22 at 15:43
  • 1
    "this redirects the show.php?id=$1 to domain.com/abc but with above category rule it is not working." - No it doesn't; it does the exact opposite. It "internally rewrites" `/abc` to `show.php?id=abc`. There are no "redirects" here (as in a 3xx external redirect). – MrWhite Apr 23 '22 at 15:45
  • Well, actually I want to remove the slug "category" from RewriteRule ^category/(.*)$ catlinks.php?id=$1 [L] which I can't figure out – Ahmad Wahid Apr 23 '22 at 16:01
  • As MrWhite says, the browser requests a URL, and the server works out what to do with that URL. It can't know that the same URL might mean one thing and might mean another. Have a look at the linked reference to understand the principles. – IMSoP Apr 23 '22 at 16:07
  • I understand, the server won't know what it would mean for one URL. But, it is a requirement of client and I will make sure both instances have different URLs. Just need to know a way to remove category slug and make both same (if possible) – Ahmad Wahid Apr 23 '22 at 16:17
  • @AhmadWahid If *you* know which URLs should match which rule, then you need to *tell Apache* that, by designing the rules appropriately. It's not going to phone you up and ask you each time someone visits the site. – IMSoP Apr 28 '22 at 20:43

0 Answers0