skip to Main Content

My url is

http://localhost/site.in/names-in.php?loc=kochi

My required url is

http://localhost/site.in/names-in/kochi

I have this in .htaccess which unfortunately not working.

Options +FollowSymLinks
RewriteEngine on
RewriteRule names-in/(.*)/ names-in.php?loc=$1  

I have this in page print_r($_GET); which shows empty array. Any inputs why its not working?

2

Answers


  1. Try this

    Options +FollowSymLinks
    RewriteEngine On
    
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteCond %{SCRIPT_FILENAME} !-f
    
    RewriteRule ^names-in/(d+)*$ ./names-in.php?loc=$1
    
    #RewriteRule ^names-in/(.*)$ ./names.php.php?loc=$1
    

    you can access from http://localhost/site.in/names-in/kochi

    Login or Signup to reply.
  2. Your original RewriteRule is not being met. You can achieve this using {QUERY_STRING} instead:

    RewirteEngine On
    RewriteCond %{QUERY_STRING} loc=(.+)$ [NC]
    RewriteRule ^(.*)$ /names-in/%1 [R=301,L,QSD]
    

    Then to internally rewrite the URL:

    RewriteRule ^/names-in/([^/]*) /names-in.php?loc=$1 [L]
    

    Make sure you clear your cache before testing this. This is using R=301 which is a permanent redirection, I advise you change this to R=302 while testing, as this is temporary.

    I’ve also added ? onto the end of the rewritten URL. This is to stop the original query appearing on the end of the URL.

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