Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cut folder path from URL via .htaccess

Tags:

php

.htaccess

I'm trying to shorten my URL but sadly can't find anything that helps.

I divide my code in folders. Index is positioned at root just like my .htaccess. The folders are named like the file extensions, so php, js, css [...]

I have a link like the following:

localhost/php/getBets.php

and want it to be

localhost/getBets/

I already have the part that cut's the .php extension at the end, so here is 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 maybe tell me how I could achieve this? :) Thanks alot!

like image 757
Shiva Haze Avatar asked Oct 29 '22 12:10

Shiva Haze


1 Answers

For your required url you can use below rule in root directory it is for rewriting,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ php/$1.php [L]
like image 139
Abhishek Gurjar Avatar answered Nov 15 '22 06:11

Abhishek Gurjar