[英]Apache mod_rewrite: return a specific file if no file/directory exists
我正在编写一个使用JavaScript从数据库加载页面内容的CMS。 但是我不想在我的URL中使用哈希#
。
当请求路径/cms/post/123
,Apache应该加载文件/cms/index.html
。 但是,如果路径下已经有一个文件,例如/cms/image.png
应该返回该文件。 另一点是,例如,当请求/cms/admin
或/cms/admin/post/123
应返回文件/cms/admin.html
。 CMS的目录可能不同。
您可以尝试在/cms/
目录的htaccess文件中使用这些规则
Options -Multiviews
RewriteEngine On
# if file exists, serve the file
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# if request is for admin, load admin.html
RewriteRule ^admin/ admin.html [L]
# everything else gets routed to index.html
RewriteRule ^ index.html [L]
您可以将其放置在CMS所在的任何目录中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.