• 0

[Mod_Rewrite] hide .php from URL with query string


Question

hey,

Im struggling to get to grips with mod rewrite, and i was hoping some smart person here could point me in the right direction.

I have managed to get the pages to load without the .php extension, but when i add a query string it screws up.

I followed a tutorial which showed me how to make the query strings as product.php/category/12/id/11 but now i want it to just be product/category/12/id/11.

This is the .htaccess code i have so far (which doesnt work as i was just guesses :D)

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)/?([A-Za-z0-9-]+)/?+$ $1.php/$2

Anyone know where im going wrong?

Thanks in advance

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

try this instead

<IfModule mod_rewrite.c>
 Options +FollowSymLinks
 Options +Indexes
 RewriteEngine On
 RewriteCond %{SCRIPT_FILENAME} !-d
 RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

Link to comment
Share on other sites

  • 0

try this instead

<IfModule mod_rewrite.c>
 Options +FollowSymLinks
 Options +Indexes
 RewriteEngine On
 RewriteCond %{SCRIPT_FILENAME} !-d
 RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>

Thanks for the reply.

I tried that code, and but only works if i go to /products

if i try to go to /products/id/12 i get 404 page not found

I need it so that it knows "/products" is the page and "id/12" is the query string.

Thanks

Link to comment
Share on other sites

  • 0

RewriteRule ^products/id/(.*) product.php?id=$1

Add that to your .htaccess file note that you may need to acess the file later on to add on different access protocols like :

RewriteRule ^people/(.*) people.php?p=$1

use the following link as ref.: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

thanks for the reply.

I actually managed to get something working, that does what i need it to. If anyone's interested here's the code.

RewriteEngine On 
RewriteCond %{HTTP_HOST} ^www.domain.co.uk$ [NC]
RewriteRule ^(.*)$ http://domain.co.uk/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/(.*)+?$  $1.php?$2

This work fine as long as the url is like "domain.co.uk/product/id/12" but for some reason, if you have "domain.co.uk/product" it redirects to domain.co.uk/product.php/" and i havent a clue why its doing it.

does anyone know the code to make it automatically redirect if the url doesnt end in "/" so "domain.co.uk/services" redirects to "domain.co.uk/services/"?

thanks in advance

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.