htaccess url rewriting, ignoring files

↘锁芯ラ 提交于 2019-12-12 22:32:20

问题


I am trying to come up with a rewrite rule, but I am having problems.

What I need - any url that starts with /services/XXX to be redirected to /services/api.php?service=XXX

I also want to ignore any files or folders that might also match.

What I have so far:

RewriteRule ^services/([a-z]+)$ /services/api.php?service=$1 [NC, L]

But this does not work at all, it shows a 404 page saying that file is missing when I test it. Any help is much appreciated.


回答1:


To ignore the files that exists just ignore the "RewriteCond"s.

Just rewrite everything that comes into services/ to api.php?service ...

Put this into your services folder.

Be careful with your encoding, save the file as .htaccess with a encode that your server reads.

# .htaccess mod_rewrite

RewriteEngine On

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

If you don't need to avoid Files, then just go to:

# .htaccess mod_rewrite

RewriteEngine On

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

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


来源:https://stackoverflow.com/questions/29761884/htaccess-url-rewriting-ignoring-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!