0
votes

I am new to htaccess file and have the following requirement.

I have a dynamic page which handles like this

    com/index.php?postname=products
    .com/index.php?cat=news&country=india

for these pages I want SEO friendly urls like this

     .com/products/      to     .com/index.php?postname=products
      .com/news/country   to    .com/index.php?cat=news&country=india

What code do I need to put it in htaccess file?

Do I need to write RewriteCond or RewriteRule or both?

If possible please give me the sample code to do this.

3

3 Answers

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

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?cat=$1&country=$2 [L]
1
votes

Try this Example for your work.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^postname=(.*)$
RewriteRule ^index\.php$ /postname/%1? [R=301]
RewriteRule ^postname/([^-]+)$ /postname.php?id=$1 [L]

The above code will redirect

index.php?postname=products

to

index/products
1
votes

Here is the htaccess code that helps you

Save the line .htaccess in your index.php folder or root folder.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^([A-Za-z0-9_\-]+)$ index.php?postname=$1 [L]
RewriteRule ^([A-Za-z0-9_\-]+)/([A-Za-z0-9_\-]+)$ index.php?cat=$1&country=$2 [L]

</IfModule>

<IfModule mod_security.c>
   SecFilterEngine Off
   SecFilterScanPOST Off
</IfModule>

<IfModule mod_gzip.c>
mod_gzip_on No
</IfModule>

It works fine for me.