skip to Main Content

I have came accross a problem that every .htaccess query I’ve tried wasn’t worked out ! I have URLs like this:

http://www.example.com/index.php?x=product

And I want to change it to a user friendly URL like this:

http://www.example.com/product/

Or it can be:

http://www.example.com/product.php

I’ve tried this code below:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]

Now, it is redirecting perfectly, but this is not the problem. I’ve used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks…

2

Answers


  1. You can use this code in your DOCUMENT_ROOT/.htaccess file:

    RewriteEngine On
    RewriteBase /
    
    RewriteCond %{THE_REQUEST} /index.php?x=([^s&]+) [NC]
    RewriteRule ^ /%1/? [R=302,L,NE]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
    

    This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.

    Login or Signup to reply.
  2. You don’t need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.

    To have it SEO friendly make sure its in this format
    http://www.example.com/product/ not http://www.example.com/product.php
    if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn’t dynamic although it is to get better pagerank)

    Insert this in your .htaccess

    RewriteRule /(.*)/$ products.php?x=$1
    

    the (.*) pulls the elements out and puts them in variables $1

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