简体   繁体   中英

Cut folder path from URL via .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!

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]

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

Use %{REQUEST_URI}, like this example:

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

may reset with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM