简体   繁体   中英

Some of rewrite rules are not working (.htaccess)

RewriteEngine on

RewriteRule ^admin/api/(.*)$ admin/api.php?method=$1 [QSA,L]
RewriteRule ^admin/(.*)$ admin/index.php [QSA,L]

RewriteRule ^api/(.*)/$ api.php?method=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|png|gif|svg|eot|ttf|woff)$ [NC]
RewriteRule (.*) index.php [QSA,L]

I want separate API entry point for admin module, but rewrite does not work as I want. What's wrong with rewrite rules?

host.com/page - works (/index.php),

host.com/admin/page - works (/admin/index.php),

host.com/admin/api/section - executes /index.php (but expected /admin/api.php)

That is because your rules are looping as admin/.* pattern also matches admin/api.php . Have rules like this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]

RewriteRule ^admin/api/([^.]+)$ admin/api.php?method=$1 [QSA,L]

RewriteRule ^admin/([^.]+)$ admin/index.php [L]

RewriteRule ^api/([^.]+)/$ api.php?method=$1 [L]

RewriteCond %{REQUEST_URI} !\.(?:css|js|jpg|png|gif|svg|eot|ttf|woff)$ [NC]
RewriteRule (.*) index.php [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