Hi,

How to rewrite my page www.site.com/category.php?item=mouse to www.site.com/category/mouse using .htaccess
I tried many in google
Please help me
Thanks in advance

Dani AI

Generated

A compact, practical approach for turning
/category.php?item=mouse into /category/mouse is to add a focused rewrite that captures the slug and internally maps it to the script, plus a separate redirect from the old query URL to the new pretty URL for SEO. This builds on ' suggestion to use .htaccess while avoiding a full front-controller rule when only one page needs pretty URLs. If the site lives in a subdirectory (for example, the earlier /ht_test mention), use RewriteBase /subdir/ or prefix the target paths with that subdirectory.

# pretty URL -> internal script
RewriteEngine On
RewriteRule ^category/([^/]+)/?$ category.php?item=$1 [L,QSA]

# canonical: send old query URLs to pretty URLs (one-time redirect)
RewriteCond %{THE_REQUEST} \s/+category\.php\?item=([^&\s]+) [NC]
RewriteRule ^ /category/%1? [R=301,L]

Notes and troubleshooting: patterns in per-directory .htaccess do not start with a leading slash, so ^category/ is correct. The redirect uses THE_REQUEST to avoid redirect loops, and the trailing ? drops the original query string. Test with R=302 first, then switch to R=301. Ensure mod_rewrite is enabled and AllowOverride permits rewrites; add conditions to skip existing files/directories if static assets break. For authoritative behavior and per-directory specifics, see the Apache mod_rewrite documentation: mod_rewrite documentation.

Recommended Answers

All 3 Replies

To start with do you have the following within your .htaccess file:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /ht_test/index.php?url=$1 [L]

Thanks for your reply ... Let me try


Thanks
Rajeesh

To start with do you have the following within your .htaccess file:

Options +FollowSymlinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ /ht_test/index.php?url=$1 [L]

ok, let me know how you go, dont hesitate if you have more problems to post them here...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.