Home > database >  htaccess - unwanted redirection to index.php
htaccess - unwanted redirection to index.php

Time:12-21

RewriteEngine ON

# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} index\.php\?c=([^\s&] ) [NC]
RewriteRule ^ %1? [R=301,L,NE]
RewriteRule ^([^/.] )/?$ index.php?c=$1 [L,QSA]

the above works fine
now I want internally example.com/contact to be rewriten into example.com/contact.php

# contact -> contact.php
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^contact$ /contact.php? [L]

problerm - this redirects me to index.php
if I remove the first block of code (about index.php) - the second part works fine - there is no redirection to index.php
seems something is wrong with the second RewriteCond
pls help

CodePudding user response:

With your shown samples, please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact)/?$ $1.php [QSA,NC,L]

# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} \s/index\.php/?c=(\S )\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

##For internal rewrite rules here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c=$1 [L,QSA]

OR in case you have to internally rewrite to about to about.php OR contact to contact.php then you can use following rules.

RewriteEngine ON
##Rule for contact.php rewrite.
RewriteRule ^(contact|about)/?$ $1.php [QSA,NC,L]

# example.com/index.php?c=kb -> example.com/kb
RewriteCond %{THE_REQUEST} \s/index\.php/?c=(\S )\s [NC]
RewriteRule ^ %1? [R=301,L,NE]

##For internal rewrite rules here.    
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c=$1 [L,QSA]
  • Related