Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

htaccess – unwanted redirection to index.php

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]

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]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?c=$1 [L,QSA]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading