skip to Main Content

I have the following mod rewrite in my .htaccess and when I go to /commPortal.php it still ends up routing me to index2.php.

RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index2.php [QSA,L]

RewriteCond %{REQUEST_URI} .php [NC]
RewriteRule ^ index2.php [QSA,L]

This seems to be due to RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [L] then getting picked up by:

RewriteCond %{REQUEST_URI} .php [NC]
RewriteRule ^ index2.php [QSA,L]

Is there any way to get the RewriteCond %{REQUEST_URI} .php [NC] to see the commPortal.php rather than /commportal or even have it ignored if there was already a matching rewrite rule?

2

Answers


  1. Change your last rule to this:

    RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [L,QSA,NC]
    
    RewriteRule ^index2.php$ - [L,NC]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index2.php [L]
    
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule .php$ index2.php [L,NC]
    

    RewriteCond %{ENV:REDIRECT_STATUS} ^$ condition will be true only if there has been no internal rewrite before this rule thus executing this rule only no other rule has been fired before this.

    Another option to use THE_REQUEST variable that contains original request not the rewritten one:

    RewriteCond %{THE_REQUEST} !s/+commportal/ [NC]
    RewriteRule .php$ index2.php [L,NC]
    
    Login or Signup to reply.
  2. Could you please try following, based on your shown samples only. Please make sure to clear your browser cache before testing your URLs.

    RewriteRule ^commportal/(.+)$ commPortal.php?data=$1 [NC,L]
    
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index2.php [QSA,L]
    
    RewriteCond %{REQUEST_URI} !commportal.php/?$ [NC]
    RewriteCond %{REQUEST_URI} .php [NC]
    RewriteRule ^ index2.php [QSA,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search