-3

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 Answers2

0

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>
rosengrenen
  • 731
  • 6
  • 21
  • thx i got to work with this rule and links like category/firm – nicolai olsen Aug 08 '16 at 20:53
  • Glad it worked, you can add `RewriteCond %{REQUEST_FILENAME} !-d` and `RewriteCond %{REQUEST_FILENAME} !-f` between `RewriteEngine On` and `RewriteRule ...` if you want to make sure the requested url is not a folder, nor a file. – rosengrenen Aug 08 '16 at 21:33
0

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).

Community
  • 1
  • 1
Spencer D
  • 3,376
  • 2
  • 27
  • 43
  • it did rewrite the url but i couldnt get it to work with my variables. i dont know why but the internal rewrite didnt work?? – nicolai olsen Aug 08 '16 at 20:55