Cut folder path from URL via .htaccess

I am trying to shorten the URL, but unfortunately I can not find anything that will help.

I share my code in folders. The index is positioned as root in the same way as my .htaccess. Folders are called as file extensions, so php, js, css [...]

I have a link like:

localhost/php/getBets.php 

and want him to be

 localhost/getBets/ 

I already have a part that shortens the .php extension at the end, so here are my full .htacces

 Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / # Hide Index IndexIgnore * # Forbid accessing certain sites RedirectMatch 403 ^/.gitignore$ RedirectMatch 403 ^/.htaccess$ RewriteRule ^(?!index)(?!.*getBets).*\.(php|rb|py|txt|md|sql|inc)$ - [F,L,NC] # Hide .php file ending in URL RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^/]+)/$ $1.php RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ /$1/ [R=301,L] 

Can someone tell me how I could achieve this? :) Thank you very much!

+5
source share
2 answers

For your required URL, you can use the rule below in the root directory for rewriting,

 RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([\w-]+)/$ php/$1.php [L] 
+2
source

Actualy Apache still does not have pathinfo ($, PATHINFO_DIRNAME), a function like PHP.

Use% {REQUEST_URI}, as this example:

 RewriteRule ^(.+)/$ /path-dirname/$1 [R=301,L] 

can reset with:

 RewriteCond %{REQUEST_URI} ^(.+)/$ RewriteRule ^.+/$ %1 [R=301,L] 
0
source

Source: https://habr.com/ru/post/1264139/


All Articles