skip to Main Content

hi i have use many different htaccess codes, but i cant get rewrite to work.
i want this url
https://domain.com/category.php?cat=firm
to look like this url
https://domain.com/category/firm

this is my latest attempt

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([^/]*).html$ /category.php?cat=$1 [L]
ErrorDocument 404 https://seoboost.no/404page.php
RewriteCond %{HTTP_HOST} ^www.seoboost.no$
RewriteRule ^/?$ "https://seoboost.no/" [R=301]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^/?$ "https://example.com/" [R=301,L]
RewriteCond %{HTTP_USER_AGENT} libwww-perl.*
RewriteRule .* ? [F]
RewriteRule ^index.php$ / [R=301]
RewriteRule ^(.*)/index.php$ /$1/ [R=301]

i have try to delete all in my htaccess and only have this code

RewriteEngine on
RewriteRule ^([^/]*).html$ /category.php?cat=$1 [L]

but it still not working, is my server or what am i doing wrong??

2

Answers


  1. Try replacing your .htaccess with

    RewriteEngine On
    RewriteRule ^category/(.*)$ /category.php?cat=$1 [L]
    

    which will redirect yoursite.com/category/12 to yoursite.com/category.php?cat=12 internally, and the user will never see the “ugly” url. Then inside your category.php file you can access the cat by $category_id = $_GET['cat']

    A simple anchor tag looks like this:

    <a href="https://yoursite.com/category/12">Item 12</a>
    
    Login or Signup to reply.
  2. Pulling from Justin Lurman’s answer here, the following should work:

    RewriteEngine On
    
    # redirect "/category.php?cat=..." to "/category/..." to hide a directly accessed "ugly" url
    RewriteCond %{THE_REQUEST} s/category.php?cat=(.+)s [NC]
    RewriteRule ^ /category/%1? [R=301,L]
    
    # internally rewrite "/category/..." to "/category.php?cat=..."
    RewriteRule ^category/(.+)$ /category.php?id=$1 [L]
    

    As Justin noted in that answer to a similar question, you need to confirm that htaccess files are enabled/allowed in your Apache configuration and that mod_rewrite is enabled.

    Additionally, I am sure you are aware, but just in case you are not, after implementing this new htaccess redirect rule, you should change all of the links on your webpages to use clean/friendly URLs. By doing so, neither your users nor a search engine crawler will access the “ugly” URLs unless they access them directly (via a bookmark or a saved link).

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search