.htaccess redirect all sub-directories to file

2019-09-07 07:03发布

I have been trying to find a method to redirect all directories except a few to a file with a variable

Example:

site.com/test -> index.php?a=test

I want all subdirectories to be forwarded except for the following

/images
/admin
/includes

I have tried the following withough success.

RewriteCond %{REQUEST_URI} !^/images(/|$)
RewriteCond %{REQUEST_URI} !^/admin(/|$)
RewriteCond %{REQUEST_URI} !^/includes(/|$)
RewriteRule ^([\w/]*)$ index.php/?a=$1 [L]

Is there a way to complete this using .htaccess

EDIT:

I found this which is closer to what im looking for but it needs to be updated to allow access to /images /admin /includes.

Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?a=$1  [NC,L,R=301]

标签: .htaccess
1条回答
成全新的幸福
2楼-- · 2019-09-07 07:22

In .htaccess you can use this rule

RewriteEngine On
RewriteRule !\.(js|jpeg|ico|gif|jpg|png|css|xml|html|swf|zip|tar|pdf|flv|gz|wmv|avi|rar|mp3)$ index.php [L]

In your application you can get $_SERVER["REQUEST_URI"] and manipulate as you whish!

<?php
$route = explode('/', $_SERVER['REQUEST_URI']);
print_r($route);
查看更多
登录 后发表回答