I am using PHP 7.2 URL Rewrite Rules. i am making 2 links first link is "category.php?inventory_id=" and second links "show-subcategories.php?cat_id" but the result is always the first link is working missing code, please help ...

I have need URL

www.example.com/nokia-1 // category url
www.example.com/nokia/nokia-n72-3 // subcategory url
www.example.com/nokia/nokia-n72/product-1 // child subcategory url

Current URL

https://www.example.com/show-subcategories.php?cat_id=1 //category url
https://www.example.com/show-childcategories.php?sub_cat_id=1 // subcategory url
https://www.example.com/search-result.php?p_id=1 // child subcategory url

The htaccess code here...

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (\d+)$ category.php?inventory_id=$1 [L]
RewriteRule category/(\d+)$ show-subcategories.php?cat_id=$1 [L]//error part rule is not working
  • Is the "category" in your last RewriteRule directive a hangover from a previous version of your question - it doesn't appear to relate to your edited URLs? It's just that that would be significant in why "the first link is working missing code" [sic]. – MrWhite May 19 '19 at 0:13

The reason that it isn't working is that (\d+)$ matches any URL that ends in digits. All of your URLs end in digits.

You have a couple other potential problems with your rewrite rules as well.

  • I'm not sure why you would want RewriteCond %{SERVER_PORT} 80 which means that the rewrite rule will only work for HTTP but not for HTTPS. In most cases you would want to remove that condition for friendly URL rewrites.
  • Your %{REQUEST_FILENAME} conditions only apply to one of your two rules. I'd think you'd want the same checks to make sure it isn't a file, to apply to each of your rewrite rules. In general RewriteCond only applies to the very next rule. I'd recommend inverting those rules and performing a no-op to stop processing any more rewrite rules if a file or directory exists.

I'd recommend the following rewrite rules. They use [^\/]+ which means "a bunch of characters with no slashes". I've also added ^ to the start of the the rewrite rules which means "starts with".

RewriteEngine On 

# Stop processing rewrites if file or directory exists
# See https://stackoverflow.com/questions/28768719/no-op-apache-mod-rewrite-rule
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^[^\/]+-(\d+)$ category.php?inventory_id=$1 [L]
RewriteRule ^[^\/]+\/[^\/]+-(\d+)$ show-childcategories.php?sub_cat_id=$1 [L]
RewriteRule ^[^\/]+\/[^\/]+\/[^\/]+-(\d+)$ search-result.php?p_id=$1 [L]

I'd also caution against using pages that look like search results for SEO. I'm not sure exactly what you search-result.php returns, but if it shows search results that look similar to Google's, Google doesn't want them in their search results. It is bad for user experience and having pages that look like Google search results indexed could get your entire site penalized. See Matt Cutts - Search results in search results.

|improve this answer|||||
  • 1
    Minor points... there is no need to backslash escape all the literal slashes in the RewriteRule patterns. And the (relatively expensive) filesystem checks (REQUEST_FILENAME conditions) may even be superfluous here - unless there are actual files with extensions that end in a digit (unlikely, ".mp4"?)? – MrWhite May 19 '19 at 0:38
  • 1
    Even a .mp4 file wouldn't match the dash before the digits, so maybe the checks would never be needed. As far as escaping goes, slashes need to be escaped in some regular expression contexts/languages, so I always escape them to make the regexes as portable as possible. See Escaping a forward slash in a regular expression – Stephen Ostermiller May 19 '19 at 0:58
  • @MrWhite I flipped the file system lookups into a no-op so that they only need to be done once at the beginning. – Stephen Ostermiller May 19 '19 at 1:10

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteRule ^product-list-nokia-([0-9]+)/?$ /search-result.php?p_id=$1 [L,QSA,NC]
RewriteRule ^category/product-list-nokia-([0-9]+)/?$ /show-subcategories.php?cat_id=$1 [L,QSA,NC]
RewriteRule ^category/subcategory/product-list-nokia-([0-9]+)/?$ /show-childcategories.php?sub_cat_id=$1 [L,QSA,NC]

https://www.example.com/search-result.php?p_id=1 to https://www.example.com/product-list-nokia-1

RewriteRule ^product-list-nokia-([0-9]+)/?$ /search-result.php?p_id=$1 [L,QSA,NC]

https://www.example.com/show-subcategories.php?cat_id=1 to https://www.example.com/category/product-list-nokia-n72-3

RewriteRule ^category/product-list-nokia-([0-9]+)/?$ /show-subcategories.php?cat_id=$1 [L,QSA,NC]

https://www.example.com/show-childcategories.php?sub_cat_id=1 to https://www.example.com/category/subcategory/product-list-nokia-n72-3

RewriteRule ^category/subcategory/product-list-nokia-([0-9]+)/?$ /show-childcategories.php?sub_cat_id=$1 [L,QSA,NC]
|improve this answer|||||
  • hello sir i am mention the URL please check its and share this idea – Amitabh May 18 '19 at 9:21
  • Yes, thatsy I too mention that... check its working for you – Rasa Mohamed May 18 '19 at 9:36
  • 1
    www.example.com/nokia-1 // category url www.example.com/nokia/nokia-n72-3 // subcategory url www.example.com/nokia/nokia-n72/product-1 // child subcategory url – Amitabh May 18 '19 at 9:38
  • 1
    nokia category always change depending on choice user click link – Amitabh May 18 '19 at 9:40
  • its not proper working sir – Amitabh May 18 '19 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.