Mobile app version of vmapp.org
Login or Join
Marchetta884

: Remove .php and id from URL and replace with slash I have tried lots of URL rewrite rules in .htaccess but I am stuck now. I have to change this URL: products.php?id=31 to

@Marchetta884

Posted in: #Htaccess #ModRewrite #UrlRewriting

I have tried lots of URL rewrite rules in .htaccess but I am stuck now. I have to change this URL:

products.php?id=31


to

products/31


I have used:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]

RewriteCond %{THE_REQUEST} s/+products(?:.php)??id=([0-9]+) [NC]
RewriteRule ^ products/%1? [R,L]

RewriteRule ^products/([0-9]+)/?$ products.php?id= [L,QSA]

## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} s([^.]+).php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ .php [L]


Using this I get the following result:

products/31


But all images and CSS not displaying. Any ideas?

10.03% popularity Vote Up Vote Down


Login to follow query

More posts by @Marchetta884

3 Comments

Sorted by latest first Latest Oldest Best

 

@Holmes151

about the images and CSS not displaying;
If your URL gets rewritten to something like products/31
keep in mind that relative links inside the html are now relative
to products/ even though the php script itself sits in the root.
Because the html gets interpreted by the browser and the browser thinks
you are in the products/ folder.
You can use absolute urls (starting with hostname/ or just relative to root, thus starting with /) or have them rewritten too.

10% popularity Vote Up Vote Down


 

@Turnbaugh106

Try this:

RewriteEngine on
RewriteRule ^(.*) [L]
RewriteRule ^products/$ products.php?id=31 [L]


This code working fine for me.
For more details: www.askapache.com/htaccess/htaccess.html

10% popularity Vote Up Vote Down


 

@Kristi941

Try this:

RewriteRule ^(.*)/$ .php [NC]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme