skip to Main Content

original url is one of the following:

  • example.com
  • example.com/index.php
  • example.com/index.php?c=lorem

if example.com – nothing should be done
if example.com/index.php – it should be visible as example.com and here is my code – works fine:

RewriteEngine ON
RewriteRule ^index.php/?$ https://%{HTTP_HOST}/ [NC,R=301]
RewriteRule ^/?$ index.php [L]

if example.com/index.php?c=lorem it should be visible as example.com/lorem

and lorem should be accessible by php in both cases:

  • if a user type example.com/index.php?c=lorem or
  • a user type example.com/lorem

pls help

2

Answers


  1. You may use these rules in your site root .htaccess:

    DirectoryIndex index.php
    RewriteEngine On
    
    # remove index.php
    RewriteCond %{THE_REQUEST} /index.phps [NC]
    RewriteCond %{REQUEST_URI} ^(.*/)index.php$ [NC]
    RewriteRule ^ %1 [L,R=301,NE]
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} s/+index.php?c=([^s&]+) [NC]
    RewriteRule ^ /%1? [R=301,L,NE]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([^/.]+)/?$ index.php?c=$1 [L,QSA]
    
    Login or Signup to reply.
  2. Could you please try following, based on your shown samples.
    Please do clear your browser cache before testing your URLs.

    RewriteEngine ON
    ##This rule is for handling index.php file in uri.
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^index.php/?$ / [R=301,NC,L]
       
    ##This rule is to redirect from index.php?c=lorem to lorem in uri.
    RewriteCond %{THE_REQUEST} index.php?c=(lorem)s [NC]
    RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
    
    ##Rule for non-existing files/directories to index.php with variables.
    RewriteCond  %{REQUEST_FILENAME} !-f
    RewriteCond  %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?c=$1 [L]
    


    OR: Above is for specifically lorem in case your query string could be anything then try following.

    RewriteEngine ON
    ##This rule is for handling index.php file in uri.
    RewriteCond %{QUERY_STRING} ^$
    RewriteRule ^index.php/?$ / [R=301,NC,L]
       
    ##This rule is to redirect from index.php?c=lorem to lorem in uri.
    RewriteCond %{THE_REQUEST} index.php?c=([^s&]*) [NC]
    RewriteRule ^ http://%{HTTP_HOST}/%1? [R=301,L,NE]
    
    ##Rule for non-existing files/directories to index.php with variables.
    RewriteCond  %{REQUEST_FILENAME} !-f
    RewriteCond  %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?c=$1 [L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search