php how to force redirect in case of typo with many slashes

Viewed 50

I have issue, this is correct address : http://localhost/mkv4/index.php

But when someone made typo like below http://localhost/mkv4/indexdsadsadsadsa.php

Page will be redirected to: http://localhost/mkv4/index.php

And this is okay, i set up earlier in .htaccess and it works, and its fine.

But when someone make something like this: http://localhost/mkv4/index.php/dasdsadasdsadsa/dsadsaddsaads/ Then i received index.php, but without css, and url is still with typo, how i can force redirect to index.php, and change url in address bar, no matter how many slashes is in typo? Sorry but i will gonna crazy soon. Thanks in advance for any help Regards

EDIT

I have same basic handmade js/php/mysql site without any frameworks/library I have below .htaccess code

ErrorDocument 404 /index.php 
ErrorDocument 500 /index.php 

RewriteEngine On  
RewriteCond %{REQUEST_FILENAME} !-d  
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^projekt/([0-9a-zA-Z_-]+) projekt.php?projekt_url=$1 [NC,L]  

RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 
RewriteRule ^.*$ 404.php [NC,L]  

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^(.*)/$ /$1 [R=301,L] 
 
FallbackResource /index.php 

And now i would to redirect to /index.php in case any typo and in case every 404 error, i would like to change url to /index.php everytime after redirect.

But i noticed three scenarios:

  1. In case localhost/mkv4/indsadsadasads everything redirect fine to localhost/mkv4/index.php and url has been changed so it works perfectly in that case.
  2. In case localhost/mkv4/ingggfdfgdsfgd/dsadsaadsdsa/index.php I got get infinite redirect loop error :( Could you help me how can i fix that?
  3. In case localhost/mkv4/index.php/projekt/projekt/projekt/session-m-m I got index.php but without css, and url is still broken :( Could you assist again ? :)

Thanks in advance

1 Answers

Simply use a RewriteRule to remove the trailing slash(es) from url

Use the following .htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
Related